[英]Getting the text content of an XML element without getting the text content of its child nodes
[英]Issue with XML child nodes iteration when mix of text and element nodes
我試圖解析以下字符串以形成xml文檔,然后嘗試提取的所有子節點並將其添加到我已經可以使用的另一個文檔對象中。
<dhruba><test>this</test>that<test2>wang chu</test2> something.... </dhruba>
<dhruba>this is text node <test>this</test>that<test2>wang chu</test2> anything..</dhruba>
當我嘗試讀取子節點時,它為第一個字符串返回TEXT_NODE的空子節點,為第二個字符串返回ELEMENT_NODE的空子節點,這是錯誤的,這是API問題嗎?
我正在使用以下代碼...它正在編譯,我正在使用Java 6。
Node n = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
dom = db.newDocument();
Element rootEle = dom.createElement("resources");
// adding the root element to the document
dom.appendChild(rootEle);
Element element = dom.createElement("string");
element.setAttribute("name", "some_name");
try {
n = db.parse(new InputSource(new StringReader("<dhruba><test>this</test>that<test2>node value</test2> some text</dhruba>"))).getDocumentElement();
n = dom.importNode(n, true);
NodeList nodeList = n.getChildNodes();
int length = nodeList.getLength();
System.out.println("Total no of childs : "+length);
for(int count = 0 ; count < length ; count++ ){
Node node = nodeList.item(count);
if(node != null ){
element.appendChild(node);
}
}
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rootEle.appendChild(element);
輸入::作為字符串
<dhruba><string name="some_name">
that
<test>this</test>
<test2>node value</test2>
some text
</string>
</dhruba>
預期輸出::作為文檔
<string>
<string name="some_name">
<test>this</test>
<test2>node value</test2>
</string>
</string>
如果我嘗試解析
<test>this</test>that<test2>wang chu</test2> something....
然后輸出為“ thiswang chu”
Why is this happening? what needs to be done if I want to add following node under another document element, i.e. <string>.
<test>this</test>
that
<test2>node value</test2>
some text
[notice that it does not have <dhruba>] inside parent node of another
document.
希望我清楚。 上面的代碼在Java 6中編譯
我將假定這是Java。
首先,我很驚訝您的importNode()
調用沒有異常,因為您正在導入Document
,這是不允許的(根據JavaDoc)。
現在問您一個問題:如果只想附加特定的節點類型,則需要使用該節點的類型進行測試。 switch
語句是最簡單的(注意:此語句尚未編譯,可能包含語法錯誤):
switch (n.getNodeType())
{
case ELEMENT_NODE :
// append the node to the other tree
break;
default :
// do nothing
}
可能您需要Node.cloneNode()
方法:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.newDocument();
Element element = dom.createElement("string");
element.setAttribute("name", "some_name");
String inputXMLString =
"<dhruba><test>this</test>that<test2>node value</test2> some text</dhruba>";
Node n = db.parse(new InputSource(new StringReader(inputXMLString))).getDocumentElement();
n = dom.importNode(n, true);
NodeList nodeList = n.getChildNodes();
for (int i = 0; i < nodeList.getLength(); ++i)
{
Node node = nodeList.item(i);
element.appendChild(node.cloneNode(true));
}
dom.appendChild(element);
要使dom
進入stdout或文件,您可以編寫:
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
DOMSource source = new DOMSource(dom);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
結果:
<string name="some_name">
<test>this</test>that<test2>node value</test2> some text</string>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.