我正在解析一个作为InputStream读取的xml文档,我看到一个示例首先将流放入javax.xml.transform.stream.StreamSource。为什么要这样做,而不是在读入时直接解析流?Java API的描述没有帮助:"充当转换源的持有者,以XML标记流的形式呈现。"
使用StreamSource的示例:
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();