"읽기"파일의 인터넷 상에 많은 예제가 있지만 노드 값을 "편집"하고 다시 작성하는 데는 아무 것도 찾을 수 없습니다 원본 파일로. Java를 사용하여 파일 객체의 XML 노드를 편집하는 방법
나는이처럼 보이는 작동하지 않는 XML 작가 클래스가이 예를 들어import org.w3c.dom.Document;
public class RunIt {
public static Document xmlDocument;
public static void main(String[] args)
throws TransformerException, IOException {
try {
xmlDocument = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse("thor.xml");
} catch (IOException ex) {
ex.printStackTrace();
} catch (SAXException ex) {
ex.printStackTrace();
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
}
addElement("A", "New");
writeDoc();
}
public static void addElement(String path, String val){
Element e = xmlDocument.createElement(path);
e.appendChild(xmlDocument.createTextNode(val));
xmlDocument.getDocumentElement().appendChild(e);
}
public static void writeDoc() throws TransformerException, IOException {
StringWriter writer = new StringWriter();
Transformer tf;
try {
tf = TransformerFactory.newInstance().newTransformer();
tf.transform(new DOMSource(xmlDocument), new StreamResult(writer));
writer.close();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
}
}
}
을, 이것은 XML 말할 수 있습니다 나는 내부 (는 "C"노드를 추가 할 하여 값 "새" 포함하는 노드) :
<A>
<B>Original</B>
</A>
read this이 내가 생각 해낸 대답은 : http://thegreenoak.blogspot.com/2012/07/writing-string-value-to-xml-file -using.html – djangofan