我正在使用此代码来解析xml
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(data));
Document doc = db.parse(is);
Run Code Online (Sandbox Code Playgroud)
现在我想从xml节点获取所有内容.喜欢这个xml
<?xml version='1.0'?>
<type>
<human>
<Name>John Smith</Name>
<Address>1/3A South Garden</Address>
</human>
</type>
Run Code Online (Sandbox Code Playgroud)
所以如果想要获得所有内容的<human>文本.
Run Code Online (Sandbox Code Playgroud)<Name>John Smith</Name> <Address>1/3A South Garden</Address>
我怎么才能得到它 ?
Rup*_*pok 29
private String nodeToString(Node node) {
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
System.out.println("nodeToString Transformer Exception");
}
return sw.toString();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15633 次 |
| 最近记录: |