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


Java XMLInputFactory.newInstance方法代码示例

本文整理汇总了Java中javax.xml.stream.XMLInputFactory.newInstance方法的典型用法代码示例。如果您正苦于以下问题:Java XMLInputFactory.newInstance方法的具体用法?Java XMLInputFactory.newInstance怎么用?Java XMLInputFactory.newInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.xml.stream.XMLInputFactory的用法示例。


在下文中一共展示了XMLInputFactory.newInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testXMLStreamWriter

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
/**
 * Test XMLStreamWriter parsing a file with an external entity reference.
 */
@Test
public void testXMLStreamWriter() {

    try {
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(System.out);
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        String file = getClass().getResource("XMLEventWriterTest.xml").getPath();
        XMLEventReader eventReader = inputFactory.createXMLEventReader(new StreamSource(new File(file)));

        // adds the event to the consumer.
        eventWriter.add(eventReader);
        eventWriter.flush();
        eventWriter.close();

        // expected success
    } catch (Exception exception) {
        exception.printStackTrace();
        Assert.fail(exception.toString());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:XMLEventWriterTest.java

示例2: testXml

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testXml() throws IOException, XMLStreamException {
    String xml = "<?xml version=\"1.0\" ?><index name=\"transient\"><j>0.5</j></index>";
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    TransientSecurityIndex index;
    try (Reader reader = new StringReader(xml)) {
        XMLStreamReader xmlReader = xmlif.createXMLStreamReader(reader);
        try {
            index = TransientSecurityIndex.fromXml("c1", xmlReader);
        } finally {
            xmlReader.close();
        }
    }
    assertTrue(index.getJ() == 0.5d);
    assertEquals(xml, index.toXml());
}
 
开发者ID:powsybl,项目名称:powsybl-core,代码行数:17,代码来源:TransientSecurityIndexTest.java

示例3: testXpathUnmarshaller

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
/**
 * Test method for DomainMetadataResultXpathUnmarshaller
 */
@Test
public final void testXpathUnmarshaller() throws Exception {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(DomainMetadataResultUnmarshallerTest.class
                                                                              .getResourceAsStream("DomainMetadataResponse.xml"));
    StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(eventReader);
    DomainMetadataResponse result = new DomainMetadataResponseUnmarshaller()
            .unmarshall(unmarshallerContext);

    assertTrue(result.itemCount() == 25);
    assertTrue(result.itemNamesSizeBytes() == 12345);
    assertTrue(result.attributeNameCount() == 20);
    assertTrue(result.attributeNamesSizeBytes() == 2345);
    assertTrue(result.attributeValueCount() == 25);
    assertTrue(result.attributeValuesSizeBytes() == 1234);
    assertTrue(result.timestamp() == 5555);
}
 
开发者ID:aws,项目名称:aws-sdk-java-v2,代码行数:21,代码来源:DomainMetadataResultUnmarshallerTest.java

示例4: testPITargetAndData

import javax.xml.stream.XMLInputFactory; //导入方法依赖的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();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:ProcessingInstructionTest.java

示例5: testXml

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testXml() throws IOException, XMLStreamException {
    String xml = "<?xml version=\"1.0\" ?><index name=\"tso-undervoltage\"><computation-succeed>true</computation-succeed><undervoltage-count>1</undervoltage-count></index>";
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    TsoUndervoltageSecurityIndex index;
    try (Reader reader = new StringReader(xml)) {
        XMLStreamReader xmlReader = xmlif.createXMLStreamReader(reader);
        try {
            index = TsoUndervoltageSecurityIndex.fromXml("c1", xmlReader);
        } finally {
            xmlReader.close();
        }
    }
    assertTrue(index.getUndervoltageCount() == 1);
    assertEquals(xml, index.toXml());
}
 
开发者ID:powsybl,项目名称:powsybl-core,代码行数:17,代码来源:TsoUndervoltageSecurityIndexTest.java

示例6: 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

示例7: testXml

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testXml() throws IOException, XMLStreamException {
    String xml = "<?xml version=\"1.0\" ?><index name=\"tso-overload\"><computation-succeed>true</computation-succeed><overload-count>1</overload-count></index>";
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    TsoOverloadSecurityIndex index;
    try (Reader reader = new StringReader(xml)) {
        XMLStreamReader xmlReader = xmlif.createXMLStreamReader(reader);
        try {
            index = TsoOverloadSecurityIndex.fromXml("c1", xmlReader);
        } finally {
            xmlReader.close();
        }
    }
    assertTrue(index.getOverloadCount() == 1);
    assertEquals(xml, index.toXml());
}
 
开发者ID:powsybl,项目名称:powsybl-core,代码行数:17,代码来源:TsoOverloadSecurityIndexTest.java

示例8: testUnmarshall

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
/**
 * Test method for GetAttributesResponseUnmarshaller
 */
@Test
public final void testUnmarshall() throws Exception {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(DomainMetadataResultUnmarshallerTest.class
                                                                              .getResourceAsStream("GetAttributesResponse.xml"));
    StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(eventReader);
    GetAttributesResponse result = new GetAttributesResponseUnmarshaller()
            .unmarshall(unmarshallerContext);

    assertTrue(!result.attributes().isEmpty());
    assertTrue(result.attributes().size() == 2);
    assertTrue(result.attributes().get(0).name().equals("Color"));
    assertTrue(result.attributes().get(0).value().equals("Blue"));
    assertTrue(result.attributes().get(1).name().equals("Price"));
    assertTrue(result.attributes().get(1).value().equals("$2.50"));
}
 
开发者ID:aws,项目名称:aws-sdk-java-v2,代码行数:20,代码来源:GetAttributesResultUnmarshallerTest.java

示例9: testStartElement

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testStartElement() {
    try {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader xsr = xif.createXMLStreamReader(this.getClass().getResource(INPUT_FILE).toExternalForm(),
                this.getClass().getResourceAsStream(INPUT_FILE));

        while (xsr.hasNext()) {
            int event = xsr.next();
            if (event == XMLStreamReader.START_ELEMENT) {
                String localName = xsr.getLocalName();
                boolean print = "para".equals(localName);
                int nrOfAttr = xsr.getAttributeCount();
                if (print) {
                    Assert.assertTrue(nrOfAttr > 0, "Default attribute declared in DTD is missing");
                }

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:Bug6370703.java

示例10: FreeColXMLReader

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
/**
 * Creates a new {@code FreeColXMLReader}.
 *
 * @param reader A {@code Reader} to create
 *     an {@code FreeColXMLReader} for.
 * @exception XMLStreamException if thrown while creating the reader.
 */
public FreeColXMLReader(Reader reader) throws XMLStreamException {
    super();

    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(reader);
    setParent(xsr);
    this.inputStream = null;
    this.readScope = ReadScope.NORMAL;
    this.uninterned.clear();
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:18,代码来源:FreeColXMLReader.java

示例11: testEventReader

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testEventReader() {
    try {
        XMLInputFactory ifac = XMLInputFactory.newInstance();
        XMLEventReader read = ifac.createXMLEventReader(this.getClass().getResource(INPUT_FILE).toExternalForm(),
                this.getClass().getResourceAsStream(INPUT_FILE));
        while (read.hasNext()) {
            XMLEvent event = read.nextEvent();
            if (event.isStartElement()) {
                StartElement startElement = event.asStartElement();
                if (startElement.getName().getLocalPart().equals("bookurn")) {
                    Iterator iterator = startElement.getNamespaces();
                    int count = 0;
                    while (iterator.hasNext()) {
                        iterator.next();
                        count++;
                    }
                    Assert.assertTrue(count == 2, "Two namespaces are expected for <bookurn> ");

                    Iterator attributes = startElement.getAttributes();
                    count = 0;
                    while (attributes.hasNext()) {
                        iterator.next();
                        count++;
                    }
                    Assert.assertTrue(count == 0, "Zero attributes are expected for <bookurn> ");
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:DefaultAttributeTest.java

示例12: newXMLInputFactory

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
public static XMLInputFactory newXMLInputFactory(boolean disableSecurity)  {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    if (xmlSecurityDisabled(disableSecurity)) {
        // TODO-Miran: are those apppropriate defaults?
        factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    }
    return factory;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:XmlUtil.java

示例13: testOne

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testOne() {
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createFilteredReader(factory.createXMLStreamReader(new StringReader(XML)), new Filter());
        reader.next();
        reader.hasNext(); // force filter to cache
        Assert.assertTrue(reader.getAttributeLocalName(0) != null);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Unexpected Exception: " + e.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:AttributeLocalNameTest.java

示例14: test1

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void test1() {
    try {
        XMLInputFactory xif = XMLInputFactory.newInstance();

        XMLStreamReader xsr = xif.createXMLStreamReader(getClass().getResource("sgml.xml").toString(), getClass().getResourceAsStream("sgml.xml"));

        XMLEventReader xer = xif.createXMLEventReader(xsr);

        Assert.assertTrue(xer.nextEvent().getEventType() == XMLEvent.START_DOCUMENT);
        xsr.close();
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:Bug6489890.java

示例15: testStandaloneSet

import javax.xml.stream.XMLInputFactory; //导入方法依赖的package包/类
@Test
public void testStandaloneSet() {
    final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><prefix:root xmlns=\"\" xmlns:null=\"\"></prefix:root>";

    try {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader r = xif.createXMLStreamReader(new StringReader(xml));
        Assert.assertTrue(!r.standaloneSet() && !r.isStandalone());

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:Issue47Test.java


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