[英]What's wrong with this Java XML-Parsing code?
我正在嘗試解析XML文件,並能夠插入路徑並獲取字段的值。
它看起來如下:
import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class XMLConfigManager {
private Element config = null;
public XMLConfigManager(String file) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
Document domTree;
DocumentBuilder db = dbf.newDocumentBuilder();
domTree = db.parse(file);
config = domTree.getDocumentElement();
}
catch (IllegalArgumentException iae) {
iae.printStackTrace();
}
catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
catch (SAXException se) {
se.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
public String getStringValue(String path) {
String[] pathArray = path.split("\\|");
Element tempElement = config;
NodeList tempNodeList = null;
for (int i = 0; i < pathArray.length; i++) {
if (i == 0) {
if (tempElement.getNodeName().equals(pathArray[0])) {
System.out.println("First element is correct, do nothing here (just in next step)");
}
else {
return "**This node does not exist**";
}
}
else {
tempNodeList = tempElement.getChildNodes();
tempElement = getChildElement(pathArray[i],tempNodeList);
}
}
return tempElement.getNodeValue();
}
private Element getChildElement(String identifier, NodeList nl) {
String tempNodeName = null;
for (int i = 0; i < nl.getLength(); i++) {
tempNodeName = nl.item(i).getNodeName();
if (tempNodeName.equals(identifier)) {
Element returner = (Element)nl.item(i).getChildNodes();
return returner;
}
}
return null;
}
}
XML看起來像這樣(出於測試目的):
<?xml version="1.0" encoding="UTF-8"?>
<amc>
<controller>
<someOtherTest>bla</someOtherTest>
<general>
<spam>This is test return String</spam>
<interval>1000</interval>
</general>
</controller>
<agent>
<name>test</name>
<ifc>ifcTest</ifc>
</agent>
</amc>
現在我可以這樣叫課了
XMLConfigManager xmlcm = new XMLConfigManager("myConfig.xml");
System.out.println(xmlcm.getStringValue("amc|controller|general|spam"));
在這里,我期望標記為spam
的值,因此它將是“ This is test return String
”。 但是我越來越null
。
我已經嘗試修復此問題好幾天了,但我還是無法解決。 迭代有效,因此它到達了標記spam
,但是然后,正如我所說的,它返回null
而不是文本。
這是錯誤還是我做錯了? 為什么? :(
非常感謝您的幫助!
問候,Flo
您正在調用Node.getNodeValue()
-據記錄,當您在元素上調用它時,它會返回null。 您應該改為調用getTextContent()
-或使用更高級別的API。
正如其他人在我之前提到的那樣,您似乎正在重塑XPath的概念。 您可以將代碼替換為以下內容:
javax.xml.xpath.XPath xpath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
String expression = "/amc/controller/general/spam";
org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource("myConfig.xml");
String result = xpath.evaluate(expression, inputSource);
另請參閱: J2SE 5.0中的XML驗證和XPath評估
編輯:
使用XPath提取集合的示例:
NodeList result = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
for (int i = 0; i < result.getLength(); i++) {
System.out.println(result.item(i).getTextContent());
}
這里記錄了 javax.xml.xpath.XPath
接口,並且在前面的文章中還有更多示例。
此外,還有一些用於XML操作的第三方庫,您可能會發現它們更方便,例如dom4j (由duffymo建議)或JDOM 。 無論使用哪種庫,都可以利用功能強大的XPath語言。
因為您使用的是getNodeValue()
而不是getTextContent()
。
手動執行此操作是等待發生的意外; 可以使用內置的XPath解決方案,也可以使用@duffymo建議的第三方庫。 國際海事組織(IMO)在這種情況下,重新發明不會增加價值。
我想知道為什么您不使用dom4j和內置XPath之類的庫。 您正在使用非常低級的API(WC3 DOM)進行大量工作。
逐步調試,並查看該<spam>
節點具有哪些子級。 您應該迅速弄清楚為什么它為空。 比這里要快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.