我正在使用java解析xml文档。我尝试从特殊节点获取完整的文本。
这是我的xml文档
<xml><archi><test>asd</test></archi><brchi val="a"><asd>Hello</asd><bsd>World</bsd></brchi></xml>
我在这里解析文档:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new URL(url).openStream());
doc.getDocumentElement().normalize();
Element root = doc.getDocumentElement();
// loop through each item
NodeList nList = doc.getElementsByTagName("brchi");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp); ...
这很有效。但现在我想从我解析的元素中获取完整的文本。在这里&#34; brchi&#34;。这应该是结果:
<brchi val="a"><asd>Hello</asd><bsd>World</bsd></brchi>
但是我怎样才能获得文本而不是解析元素?
由于
答案 0 :(得分:0)
可能有一种更清洁的方式,但我所做的是给出了XML:
<book ISBN="0123456001">
<title>Java For Dummies</title>
<author>Tan Ah Teck</author>
<category>Programming</category>
<year>2009</year>
<edition>7</edition>
<price>19.99</price>
</book>
这是我用来提取标题文本的代码
NodeList list = doc.getElementsByTagName("title");
Element element = (Element)list.item(0)
System.out.println("\tTitle:\t"
+ element.getChildNodes().item(0).getNodeValue());`
答案 1 :(得分:0)
试试这个:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("data.xml"));
NodeList list = doc.getElementsByTagName("brchi");
Node node = list.item(0);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(node);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}