2012-03-02 2 views
0

Java 프로그램을 사용하여 런타임시 동적으로 xml 파일을 편집 할 수있는 방법은 무엇입니까? 예를 들어, 이런 XML 파일이 있습니다.런타임시 동적으로 xml 파일 업데이트

런타임시 xml을 업데이트하기 위해 jdom을 사용해 보았습니다. 그러나 단일 값 태그 만 편집 할 수 있습니다. 하지만 여기에는 같은 이름의 멀티 태그가 있습니다. 런타임시 모든 태그에 대해 값을 동적으로 변경하려고합니다. 아무도 제발 어떤 아이디어 나 제안을 제안 할 수 있습니다.

답변

0

당신이 JDOM-1.1.2.jar

Document doc = (Document) builder.build(YourFileName); 
Element rootNode = doc.getRootElement(); 
List<Element> childrenNode = rootNode.getChildren(); 
     for (Element child : childrenNode) { 
      System.out.println(child.getAttribute("value").getIntValue()); 
      child.getAttribute("value").setValue("2"); 
     } 

// print updates to xml file with good formatting 
XMLOutputter xmlOutput = new XMLOutputter(); 
xmlOutput.setFormat(Format.getPrettyFormat()); 
xmlOutput.output(doc, new FileWriter(YourFileName)); 
+0

를 사용하는 가정은 당신의 대답 친구 주셔서 감사합니다. dom4j API를 사용하여이 작업을 수행했습니다. 답변을 주셔서 감사합니다. –

관련 문제