0
更換XML處理在Java中使用XML我的第一個步驟後,我現在在這裏我想在我的XML/GPX文件更新一些數據點...中的文件,從「文件」中的Java
在我的「文檔」數據類型重新打開它的工作原理:)
問題:如何將更改的「文檔」模型存儲回我的文件?我是否必須通過使用標準文件功能(通過蒸汽等)來實現這一點oder是一個更優雅的方式來做到這一點? ;-)
這是我已經計算出的代碼,也許可以幫助。 (該方法getParsedXML只是puting從文件轉換成一個額外的方法)
Document tmpDoc = getParsedXML(currentGPX);
//XML Parsind tests:
// Access to tag attribute <tag attribut="bla">
System.out.println(tmpDoc.getElementsByTagName("wpt").item(0).getAttributes().getNamedItem("lat").getTextContent());
// Access to the value of an child element <a><CHILD>ValueOfChild</CHILD></a>
System.out.println(tmpDoc.getElementsByTagName("wpt").item(0).getChildNodes().item(5).getTextContent());
// Replacing access to tag attribute
tmpDoc.getElementsByTagName("wpt").item(0).getAttributes().getNamedItem("lat").setTextContent("139.921055008");
System.out.println(tmpDoc.getElementsByTagName("wpt").item(0).getAttributes().getNamedItem("lat").getTextContent());
// Replacing access to child element value
tmpDoc.getElementsByTagName("wpt").item(0).getChildNodes().item(5).setTextContent("Cala Sant Vicenç - Mallorca 2");
System.out.println(tmpDoc.getElementsByTagName("wpt").item(0).getChildNodes().item(5).getTextContent());
嗨,謝謝,這可能不是最好的解決方案,但它的工作原理;-) – poeschlorn 2010-05-07 13:20:52