當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。