当前位置: 首页>>代码示例>>Java>>正文


Java XMLInputFactory.createXMLStreamReader方法代码示例

本文整理汇总了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);
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:IssueTracker70.java

示例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();
}
 
开发者ID:wintertime,项目名称:FreeCol,代码行数:24,代码来源:FreeColXMLReader.java

示例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 + ")");
    }

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:Bug6847819Test.java

示例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());
}
 
开发者ID:powsybl,项目名称:powsybl-core,代码行数:18,代码来源:TsoGeneratorSpeedAutomatonTest.java

示例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();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:Bug6472982Test.java

示例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
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:StreamReaderTest.java

示例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();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:NamespaceTest.java

示例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());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:Jsr173MR1Req8Test.java

示例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());
}
 
开发者ID:powsybl,项目名称:powsybl-core,代码行数:17,代码来源:TsoOvervoltageSecurityIndexTest.java

示例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);
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:15,代码来源:SourceHttpMessageConverter.java

示例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();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:NamespaceTest.java

示例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");
	}
}
 
开发者ID:digitalproteomics,项目名称:dp-mzml,代码行数:13,代码来源:MzMLStAXParser.java

示例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());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:Jsr173MR1Req5Test.java

示例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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:10,代码来源:ExternalMetadataReader.java

示例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();
        }
    }
 
开发者ID:blackarbiter,项目名称:Android_Code_Arbiter,代码行数:11,代码来源:XmlInputFactoryVulnerable.java


注:本文中的javax.xml.stream.XMLInputFactory.createXMLStreamReader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。