本文整理汇总了Java中javax.xml.stream.XMLInputFactory.createXMLStreamReader方法的典型用法代码示例。如果您正苦于以下问题:Java XMLInputFactory.createXMLStreamReader方法的具体用法?Java XMLInputFactory.createXMLStreamReader怎么用?Java XMLInputFactory.createXMLStreamReader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLInputFactory
的用法示例。
在下文中一共展示了XMLInputFactory.createXMLStreamReader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetAttributeValueWithNs
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
private void testGetAttributeValueWithNs(String nameSpace, String attrName, Consumer<String> checker) throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(testFile));
while (xsr.hasNext()) {
xsr.next();
if (xsr.isStartElement()) {
String v;
v = xsr.getAttributeValue(nameSpace, attrName);
checker.accept(v);
}
}
}
示例2: FreeColXMLReader
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
/**
* Creates a new {@code FreeColXMLReader}.
*
* @param bis The {@code BufferedInputStream} to create
* an {@code FreeColXMLReader} for.
* @exception XMLStreamException can be thrown while creating the reader.
*/
public FreeColXMLReader(BufferedInputStream bis)
throws XMLStreamException {
super();
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr;
try {
xsr = xif.createXMLStreamReader(bis, "UTF-8");
setParent(xsr);
} catch (Exception ex) {
throw new XMLStreamException("Stream reader fail", ex);
}
this.inputStream = bis;
this.readScope = ReadScope.NORMAL;
this.uninterned.clear();
}
示例3: testIllegalDecl
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testIllegalDecl() throws XMLStreamException {
String xml = "<?xml ?><root>abc]]>xyz</root>";
String msg = "illegal declaration";
try {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.createXMLStreamReader(new StringReader(xml));
Assert.fail("Expected an exception for " + msg);
} catch (XMLStreamException ex) { // good
System.out.println("Expected failure: '" + ex.getMessage() + "' " + "(matching message: '" + msg + "')");
} catch (Exception ex2) { // ok; iff links to XMLStreamException
Throwable t = ex2;
while (t.getCause() != null && !(t instanceof XMLStreamException)) {
t = t.getCause();
}
if (t instanceof XMLStreamException) {
System.out.println("Expected failure: '" + ex2.getMessage() + "' " + "(matching message: '" + msg + "')");
}
if (t == ex2) {
Assert.fail("Expected an XMLStreamException (either direct, or getCause() of a primary exception) for " + msg + ", got: " + ex2);
}
Assert.fail("Expected an XMLStreamException (either direct, or getCause() of a primary exception) for " + msg + ", got: " + ex2 + " (root: " + t + ")");
}
}
示例4: testXml
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testXml() throws IOException, XMLStreamException {
String xml = "<?xml version=\"1.0\" ?><index name=\"tso-generator-speed-automaton\"><onUnderSpeedDisconnectedGenerators><gen>a</gen><gen>b</gen></onUnderSpeedDisconnectedGenerators><onOverSpeedDisconnectedGenerators><gen>c</gen></onOverSpeedDisconnectedGenerators></index>";
XMLInputFactory xmlif = XMLInputFactory.newInstance();
TsoGeneratorSpeedAutomaton index;
try (Reader reader = new StringReader(xml)) {
XMLStreamReader xmlReader = xmlif.createXMLStreamReader(reader);
try {
index = TsoGeneratorSpeedAutomaton.fromXml("c1", xmlReader);
} finally {
xmlReader.close();
}
}
assertTrue(index.getOnUnderSpeedDiconnectedGenerators().equals(Arrays.asList("a", "b")));
assertTrue(index.getOnOverSpeedDiconnectedGenerators().equals(Arrays.asList("c")));
assertEquals(xml, index.toXml());
}
示例5: testNamespaceContext
import javax.xml.stream.XMLInputFactory; //导入方法依赖的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);
NamespaceContext context = sr.getNamespaceContext();
Assert.assertTrue(context.getPrefix("") == null);
} catch (IllegalArgumentException iae) {
Assert.fail("NamespacePrefix#getPrefix() should not throw an IllegalArgumentException for empty uri. ");
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例6: testHasNameOnEntityEvent
import javax.xml.stream.XMLInputFactory; //导入方法依赖的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
}
示例7: testChildElementNamespace
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testChildElementNamespace() {
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)) {
QName qname = sr.getName();
Assert.assertTrue(qname.getPrefix().equals(prefix) && qname.getNamespaceURI().equals(namespaceURI)
&& qname.getLocalPart().equals(childElement));
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例8: testDefaultAttrNS
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testDefaultAttrNS() {
XMLInputFactory ifac = XMLInputFactory.newInstance();
try {
XMLStreamReader re = ifac.createXMLStreamReader(getClass().getResource(INPUT_FILE1).toExternalForm(),
this.getClass().getResourceAsStream(INPUT_FILE1));
while (re.hasNext()) {
int event = re.next();
if (event == XMLStreamConstants.START_ELEMENT) {
// System.out.println("#attrs = " + re.getAttributeCount());
Assert.assertTrue(re.getAttributeCount() == 2);
// This works if "" is replaced by null too
// System.out.println("attr1 = " + re.getAttributeValue("",
// "attr1"));
Assert.assertTrue(re.getAttributeValue("", "attr1").equals("pass"));
}
}
re.close();
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例9: testXml
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testXml() throws IOException, XMLStreamException {
String xml = "<?xml version=\"1.0\" ?><index name=\"tso-overvoltage\"><computation-succeed>true</computation-succeed><overvoltage-count>1</overvoltage-count></index>";
XMLInputFactory xmlif = XMLInputFactory.newInstance();
TsoOvervoltageSecurityIndex index;
try (Reader reader = new StringReader(xml)) {
XMLStreamReader xmlReader = xmlif.createXMLStreamReader(reader);
try {
index = TsoOvervoltageSecurityIndex.fromXml("c1", xmlReader);
} finally {
xmlReader.close();
}
}
assertTrue(index.getOvervoltageCount() == 1);
assertEquals(xml, index.toXml());
}
示例10: readStAXSource
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
private Source readStAXSource(InputStream body) {
try {
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, isProcessExternalEntities());
if (!isProcessExternalEntities()) {
inputFactory.setXMLResolver(NO_OP_XML_RESOLVER);
}
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body);
return new StAXSource(streamReader);
}
catch (XMLStreamException ex) {
throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
}
}
示例11: testRootElementNamespace
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testRootElementNamespace() {
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(rootElement)) {
Assert.assertTrue(sr.getNamespacePrefix(0).equals(prefix) && sr.getNamespaceURI(0).equals(namespaceURI));
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例12: FromXMLStreamIterator
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
public FromXMLStreamIterator() throws XMLStreamException {
XMLInputFactory fac = XMLInputFactory.newInstance();
try {
this.xr = fac.createXMLStreamReader(Files.newInputStream(MzMLStAXParser.this.xml, StandardOpenOption.READ));
} catch (FactoryConfigurationError | IOException e) {
LOGGER.log(Level.ERROR, e.getMessage());
System.exit(-1);
}
if (!this.moveToNextSpectrum()){
LOGGER.log(Level.WARN, "no spectrum found in mzml file");
}
}
示例13: testAttributeCountNS
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testAttributeCountNS() {
XMLInputFactory ifac = XMLInputFactory.newInstance();
try {
// Turn on NS awareness to not count xmlns as attributes
ifac.setProperty("javax.xml.stream.isNamespaceAware", Boolean.TRUE);
XMLStreamReader re = ifac.createXMLStreamReader(getClass().getResource(INPUT_FILE1).toExternalForm(),
this.getClass().getResourceAsStream(INPUT_FILE1));
while (re.hasNext()) {
int event = re.next();
if (event == XMLStreamConstants.START_ELEMENT) {
// System.out.println("#attrs = " + re.getAttributeCount());
Assert.assertTrue(re.getAttributeCount() == 1);
}
}
re.close();
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例14: documentRootNamespace
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
static String documentRootNamespace(Source src, boolean disableXmlSecurity) throws XMLStreamException {
XMLInputFactory factory;
factory = XmlUtil.newXMLInputFactory(!disableXmlSecurity);
XMLStreamReader streamReader = factory.createXMLStreamReader(src);
XMLStreamReaderUtil.nextElementContent(streamReader);
String namespaceURI = streamReader.getName().getNamespaceURI();
XMLStreamReaderUtil.close(streamReader);
return namespaceURI;
}
示例15: parseXMLdefaultValue
import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
public void parseXMLdefaultValue(InputStream input) throws XMLStreamException {
XMLInputFactory factory = XMLInputFactory.newFactory();
// factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
// factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader reader = factory.createXMLStreamReader(input);
while(reader.hasNext()) {
reader.next();
}
}