0
我有一個XML配置文件:獲取標籤和值從XML文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Config>
<useProxy>true</useProxy>
<proxyReqPass>true</proxyReqPass>
<proxyHost>proxy.net.br</proxyHost>
<proxyUser>admin</proxyUser>
<proxyPass>12345</proxyPass>
</Config>
我有數據列表()對象,每個數據()包含兩個字符串,標籤名稱和的值標籤。所以我想在這個列表中插入該XML文件的數據,像例如:
List<Data> data = new ArrayList<Data>();
File fXmlFile = new File("Config.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList elements = doc.getElementsByTagName("Config");
for (int i = 0; i < elements.getLength(); i++) {
Node nNode = elements.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
data.add(new Data(eElement.getTagName(), eElement.getTextContent()));
}
}
如果我打印的清單:
for(int i = 0; i < list.size(); i++)
System.out.println("Node: " + list.get(i).getTagName() + " Value: " + list.get(i).getTextContent());
我想要得到的結果是:
> Node: useProxy Value: true
Node: proxyReqPass Value: true
Node: proxyHost Value: proxy.net.br
Node: proxyUser Value: admin
Node: proxyPass Value: 12345
但結果是:
> Node: Config Value:
false
false
我不知道我的錯誤在哪裏,請別人幫我
剛剛發現問題,而是使用NodeList到configTags即時通訊使用節點,但無論如何,非常感謝 –