本文整理汇总了Java中javax.xml.stream.XMLStreamConstants类的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamConstants类的具体用法?Java XMLStreamConstants怎么用?Java XMLStreamConstants使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
XMLStreamConstants类属于javax.xml.stream包,在下文中一共展示了XMLStreamConstants类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readUntilEndElement
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
public static String readUntilEndElement(String endElementName, XMLStreamReader reader, XmlEventHandler eventHandler) throws XMLStreamException {
String text = null;
int event;
while (!((event = reader.next()) == XMLStreamConstants.END_ELEMENT
&& reader.getLocalName().equals(endElementName))) {
text = null;
switch (event) {
case XMLStreamConstants.START_ELEMENT:
if (eventHandler != null) {
eventHandler.onStartElement();
}
break;
case XMLStreamConstants.CHARACTERS:
text = reader.getText();
break;
default:
break;
}
}
return text;
}
示例2: readFeatureMap
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
/**
* Processes a GateDocumentFeatures or Annotation element to build a
* feature map. The element is expected to contain Feature children,
* each with a Name and Value. The reader will be returned positioned
* on the closing GateDocumentFeatures or Annotation tag.
*
* @throws XMLStreamException
*/
public static FeatureMap readFeatureMap(XMLStreamReader xsr)
throws XMLStreamException {
FeatureMap fm = Factory.newFeatureMap();
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
xsr.require(XMLStreamConstants.START_ELEMENT, null, "Feature");
Object featureName = null;
Object featureValue = null;
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
if("Name".equals(xsr.getLocalName())) {
featureName = readFeatureNameOrValue(xsr);
}
else if("Value".equals(xsr.getLocalName())) {
featureValue = readFeatureNameOrValue(xsr);
}
else {
throw new XMLStreamException("Feature element should contain "
+ "only Name and Value children", xsr.getLocation());
}
}
fm.put(featureName, featureValue);
}
return fm;
}
示例3: testRootElementNamespace
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
@Test
public void testRootElementNamespace() {
is = new java.io.ByteArrayInputStream(getXML().getBytes());
try {
XMLStreamReader sr = factory.createFilteredReader(factory.createXMLStreamReader(is), (StreamFilter) filter);
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (sr.getLocalName().equals(rootElement)) {
Assert.assertTrue(sr.getNamespacePrefix(0).equals(prefixApple) && sr.getNamespaceURI(0).equals(namespaceURIApple));
}
}
}
} catch (Exception ex) {
Assert.fail("Exception: " + ex.getMessage());
}
}
示例4: accept
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
@Override
public void accept(XMLStreamReader xr) {
if((xr.getEventType() == XMLStreamConstants.START_ELEMENT)
&& xr.getLocalName().equals("cvParam")) {
if (xr.getAttributeValue(null, "accession").equals("MS:1000511")) {
this.currHeader.setMsLevel(Integer.valueOf(xr.getAttributeValue(null, "value")));
} else if(xr.getAttributeValue(null, "accession").equals("MS:1000016")) {
// normalize to seconds if unit is a minute
double norm = xr.getAttributeValue(null, "unitAccession")
.equals("UO:0000031")
? 60.0
: 1.0;
this.currHeader.setScanStartTime(norm * Double.valueOf(xr.getAttributeValue(null, "value")));
} else if(xr.getAttributeValue(null, "accession").equals("MS:1000744")) {
this.currHeader.setSelectedIonMz(Double.valueOf(xr.getAttributeValue(null, "value")));
} else if(xr.getAttributeValue(null, "accession").equals("MS:1000041")) {
this.currHeader.setSelectedIonCharge(Integer.valueOf(xr.getAttributeValue(null, "value")));
} else if(xr.getAttributeValue(null, "accession").equals("MS:1000042")) {
this.currHeader.setSelectedIonIntensity(Double.valueOf(xr.getAttributeValue(null, "value")));
}
}
}
示例5: pullNextEvent
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
@Override
protected int pullNextEvent() {
try {
switch (in.next()) {
case XMLStreamConstants.START_DOCUMENT:
case XMLStreamConstants.START_ELEMENT:
return START_NODE;
case XMLStreamConstants.END_DOCUMENT:
case XMLStreamConstants.END_ELEMENT:
return END_NODE;
case XMLStreamConstants.CHARACTERS:
return TEXT;
case XMLStreamConstants.COMMENT:
return COMMENT;
default:
return OTHER;
}
} catch (final XMLStreamException e) {
throw new StreamException(e);
}
}
示例6: toDom4jDocument
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
private Document toDom4jDocument(XMLEventReader staxEventReader, Origin origin) {
STAXEventReader dom4jStaxEventReader = new STAXEventReader();
try {
// the dom4j converter class is touchy about comments (aka, comments make it implode)
// so wrap the event stream in a filtering stream to filter out comment events
staxEventReader = new FilteringXMLEventReader( staxEventReader ) {
@Override
protected XMLEvent filterEvent(XMLEvent event, boolean peek) {
return event.getEventType() == XMLStreamConstants.COMMENT
? null
: event;
}
};
return dom4jStaxEventReader.readDocument( staxEventReader );
}
catch (XMLStreamException e) {
throw new InvalidMappingException( "Unable to read StAX source as dom4j Document for processing", origin, e );
}
}
示例7: nextTag
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public int nextTag() throws XMLStreamException {
int tag = super.nextTag();
if (tracing) {
switch (tag) {
case XMLStreamConstants.START_ELEMENT:
System.err.println("[" + getLocalName());
break;
case XMLStreamConstants.END_ELEMENT:
System.err.println(getLocalName() + "]");
break;
default:
System.err.println((tagStrings.containsKey(tag))
? tagStrings.get(tag)
: "Weird tag: " + tag);
break;
}
}
return tag;
}
示例8: testPITargetAndData
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
@Test
public void testPITargetAndData() {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
String PITarget = "soffice";
String PIData = "WebservicesArchitecture";
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<?" + PITarget + " " + PIData + "?>" + "<foo></foo>";
// System.out.println("XML = " + xml) ;
InputStream is = new java.io.ByteArrayInputStream(xml.getBytes());
XMLStreamReader sr = xif.createXMLStreamReader(is);
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION) {
String target = sr.getPITarget();
String data = sr.getPIData();
Assert.assertTrue(target.equals(PITarget) && data.equals(PIData));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例9: setHandlersAndRoles
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
/**
* Parses the handler and role information and sets it
* on the {@link WSBinding}.
*
* @return true if <handler-chains> element present in DD
* false otherwise.
*/
protected boolean setHandlersAndRoles(WSBinding binding, XMLStreamReader reader, QName serviceName, QName portName) {
if (reader.getEventType() == XMLStreamConstants.END_ELEMENT ||
!reader.getName().equals(HandlerChainsModel.QNAME_HANDLER_CHAINS)) {
return false;
}
HandlerAnnotationInfo handlerInfo = HandlerChainsModel.parseHandlerFile(
reader, classLoader, serviceName, portName, binding);
binding.setHandlerChain(handlerInfo.getHandlers());
if (binding instanceof SOAPBinding) {
((SOAPBinding) binding).setRoles(handlerInfo.getRoles());
}
// move past </handler-chains>
XMLStreamReaderUtil.nextContent(reader);
return true;
}
示例10: testNamespaceContext
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
@Test
public void testNamespaceContext() {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes());
XMLStreamReader sr = xif.createXMLStreamReader(is);
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (sr.getLocalName().equals(childElement)) {
NamespaceContext context = sr.getNamespaceContext();
Assert.assertTrue(context.getPrefix(namespaceURI).equals(prefix));
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例11: testHasNameOnEntityEvent
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
/**
* CR 6631264 / sjsxp Issue 45:
* https://sjsxp.dev.java.net/issues/show_bug.cgi?id=45
* XMLStreamReader.hasName() should return false for ENTITY_REFERENCE
*/
@Test
public void testHasNameOnEntityEvent() throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
XMLStreamReader r = xif.createXMLStreamReader(
this.getClass().getResourceAsStream("ExternalDTD.xml"));
while (r.next() != XMLStreamConstants.ENTITY_REFERENCE) {
System.out.println("event type: " + r.getEventType());
continue;
}
if (r.hasName()) {
System.out.println("hasName returned true on ENTITY_REFERENCE event.");
}
Assert.assertFalse(r.hasName()); // fails
}
示例12: StAXSource
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
/**
* Creates a new {@link javax.xml.transform.Source} for the given
* {@link XMLStreamReader}.
*
* The XMLStreamReader must be pointing at either a
* {@link javax.xml.stream.XMLStreamConstants#START_DOCUMENT} or
* {@link javax.xml.stream.XMLStreamConstants#START_ELEMENT} event.
*
* @param reader XMLStreamReader that will be exposed as a Source
* @param eagerQuit if true, when the conversion is completed, leave the cursor to the last
* event that was fired (such as end element)
* @param inscope inscope Namespaces
* array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }
* @throws IllegalArgumentException iff the reader is null
* @throws IllegalStateException iff the reader is not pointing at either a
* START_DOCUMENT or START_ELEMENT event
*/
public StAXSource(XMLStreamReader reader, boolean eagerQuit, @NotNull String[] inscope) {
if( reader == null )
throw new IllegalArgumentException();
this.staxReader = reader;
int eventType = reader.getEventType();
if (!(eventType == XMLStreamConstants.START_DOCUMENT)
&& !(eventType == XMLStreamConstants.START_ELEMENT)) {
throw new IllegalStateException();
}
this.reader = new XMLStreamReaderToContentHandler(reader,repeater,eagerQuit,false,inscope);
super.setXMLReader(pseudoParser);
// pass a dummy InputSource. We don't care
super.setInputSource(new InputSource());
}
示例13: testReadingNamespace
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
@Test
public void testReadingNamespace() {
is = new java.io.ByteArrayInputStream(getXML().getBytes());
try {
XMLStreamReader sr = factory.createFilteredReader(factory.createXMLStreamReader(is), (StreamFilter) filter);
while (sr.hasNext()) {
int eventType = sr.getEventType();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (sr.getLocalName().equals(rootElement)) {
Assert.assertTrue(sr.getNamespacePrefix(0).equals(prefixApple) && sr.getNamespaceURI(0).equals(namespaceURIApple));
}
}
eventType = sr.next();
}
} catch (Exception ex) {
Assert.fail("Exception: " + ex.getMessage());
}
}
示例14: getNotationDecls
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
protected List getNotationDecls(){
if(fEventType == XMLStreamConstants.DTD){
if(fScanner.fDTDScanner == null) return null;
DTDGrammar grammar = ((XMLDTDScannerImpl)(fScanner.fDTDScanner)).getGrammar();
if(grammar == null) return null;
List notations = grammar.getNotationDecls();
Iterator it = notations.iterator();
ArrayList list = new ArrayList();
while(it.hasNext()){
XMLNotationDecl ni = (XMLNotationDecl)it.next();
if(ni!= null){
list.add(new NotationDeclarationImpl(ni));
}
}
return list;
}
return null;
}
示例15: testStAXSourceWEmptyNS
import javax.xml.stream.XMLStreamConstants; //导入依赖的package包/类
/**
* @bug 8152530
* Verifies that StAXSource handles empty namespace properly. NPE was thrown
* before the fix.
* @throws Exception if the test fails
*/
@Test
public final void testStAXSourceWEmptyNS() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<EntityList>\n"
+ " <Entity xmlns=\"\">\n"
+ " </Entity>\n"
+ " <Entity xmlns=\"\">\n"
+ " </Entity>\n"
+ "</EntityList> ";
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
xsr.nextTag();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT && xsr.getLocalName().equals("Entity")) {
StringWriter stringResult = new StringWriter();
t.transform(new StAXSource(xsr), new StreamResult(stringResult));
System.out.println("result: \n" + stringResult.toString());
}
}