我正在解析作为 InputStream 读入的 xml 文档,我看到的一个示例首先将流放在 javax.xml.transform.stream.StreamSource 中。当我可以解析读入的流时,为什么要这样做?Java API 的描述没有帮助:“充当 XML 标记流形式的转换源的持有者。”
流源示例:
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource reportStream =
new StreamSource(new URL("file:///myXmlDocURL.xml").openStream());
XMLStreamReader xmlReader = xif.createXMLStreamReader(reportStream);
xmlReader.nextTag();
while (xmlReader.hasNext()) {
if (xmlReader.getLocalName().equals("attributeICareAbout")) {
String tempTagValue = xmlReader.getText();
xmlReader.nextTag();
}
}
xmlReader.close();
没有 StreamSource 的示例:
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xmlReader =
xif.createXMLStreamReader(new URL("file:///myXmlDocURL.xml").openStream());
xmlReader.nextTag();
while (xmlReader.hasNext()) {
if (xmlReader.getLocalName().equals("attributeIcareAbout")) {
String tempTagValue = xmlReader.getText();
xmlReader.nextTag();
}
}
xmlReader.close();