我需要將xml文件的內容打印到某個txt文件中。下面是XML類型的樣品,我想打印出:從java中的XML獲取孫子
<log>
<logentry revision="234">
<author>SOMEGUY</author>
<date>SOME DATE</date>
<paths>
<path>asdf/asdf/adsf/asdf.zip</path>
</path>
<msg>blahblahblah</msg>
</logentry>
</log>
我能得到我所需要的,除了路徑標籤的所有信息......這是我做了什麼:
FileWriter fstream = new FileWriter("c:\\work\\output.txt");
BufferedWriter out = new BufferedWriter(fstream);
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
List list = rootNode.getChildren("logentry");
for (int i=0; i< list.size(); i++) {
Element node = (Element) list.get(i);
out.write("Revision: \n" + node.getAttributeValue("revision") + "\n\n");
out.write("Author: \n" + node.getChildText("author") + "\n\n");
out.write("Date: \n" + node.getChildText("date") + "\n\n");
out.write("Message: \n" + node.getChildText("msg"));
out.write("\n-------------------------------------------------"
+"---------------------------------------------------\n\n");
}
out.close();
那麼,我如何從這個標籤獲取信息?
P.S.隨意downvote被遺忘這一點,如果這是一個愚蠢的問題...只要你也可直接向我的答案:)
感謝
你綁JDOM?你有沒有考慮XPath是否適合解決你的問題? – 2011-06-09 20:06:48