2012-11-27 2 views
1

에 JDOM 사용하여 XML의 내부 노드의 값을 변경하는 방법 - http://www.mkyong.com/java/how-to-modify-xml-file-in-java-jdom/이 링크를 참조하여 내가 JDOM의 도움으로 XML 값을 변경 시도 자바

XML 샘플에 표시된 :

<?xml version="1.0" encoding="UTF-8"?> 
<company> 
    <staff id="1"> 
    <firstname>yong</firstname> 
    <lastname>mook kim</lastname> 
    <nickname>mkyong</nickname> 
    <salary>5000</salary> 
    </staff> 
</company> 

난 다음 XML 시나리오를 처리하는 방법을 분명 아니다 것은 :

<?xml version="1.0" encoding="UTF-8"?> 
<company> 
    <staff> 
    <firstname>yong</firstname> 
    <lastname>mook kim</lastname> 
    <nickname>mkyong</nickname> 
    <salary> 
     <basic>1000</basic> 
     <hra>150</hra> 
    </salary> 
    </staff> 
    <staff> 
    <firstname>sanjay</firstname> 
    <lastname>machani</lastname> 
    <nickname>chong</nickname> 
    <salary> 
     <basic>2000</basic> 
     <hra>200</hra> 
    </salary> 
    </staff> 
</company> 

내 직원 태그가 필요하지 않습니다 ID를 또한 내가 가진 것 칠 d 급여 태그. 하지만 Java를 사용하여 xml에서 sanjay (firstname)의 급여를 변경해야합니다.

어떤 제안이 도움이 될 것입니다.

답변

3

당신이 JDOM을 사용하는 경우이 가진 요소를 반복 할 수 있습니다

import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.List; 
import org.jdom2.Document; 
import org.jdom2.Element; 
import org.jdom2.JDOMException; 
import org.jdom2.input.SAXBuilder; 
import org.jdom2.output.Format; 
import org.jdom2.output.XMLOutputter; 

    public class yourClass{ 
    public static void main(String[] args) { 

     File xml = new File("yourFile.xml"); 
     try { 
      Document doc = (Document) new SAXBuilder().build(xml); 
      Element rootNode = doc.getRootElement(); 
      List list = rootNode.getChildren("staff"); 
      XMLOutputter xmlOut = new XMLOutputter(); 

      for (int i = 0; i < list.size(); i++) { 

       Element node = (Element) list.get(i); 
       if (node.getChildText("firstname").equals("sanjay")) 
        node.getChild("salary").getChild("basic").setText("250000"); 
       xmlOut.setFormat(Format.getPrettyFormat()); 
       xmlOut.output(doc, new FileWriter("yourFile.xml")); 
      } 
     } catch (IOException io) { 
      System.out.println(io.getMessage()); 
     } catch (JDOMException jdomex) { 
      System.out.println(jdomex.getMessage()); 
     } 
    } 

} 
3

방법 XPath를 사용하는 방법에 대한 :

public class ReadSalaryFromStaffTest { 

private String xmlData; 

@Before 
public void init() { 
    xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
    "<company>\n" + 
    " <staff>\n" + 
    " <firstname>yong</firstname>\n" + 
    " <lastname>mook kim</lastname>\n" + 
    " <nickname>mkyong</nickname>\n" + 
    " <salary>\n" + 
    "  <basic>1000</basic>\n" + 
    "  <hra>150</hra>\n" + 
    " </salary>\n" + 
    " </staff>\n" + 
    " <staff>\n" + 
    " <firstname>sanjay</firstname>\n" + 
    " <lastname>machani</lastname>\n" + 
    " <nickname>chong</nickname>\n" + 
    " <salary>\n" + 
    "  <basic>2000</basic>\n" + 
    "  <hra>200</hra>\n" + 
    " </salary>\n" + 
    " </staff>\n" + 
    "</company>"; 

} 

@Test 
public void shouldChangeBaseSalaryInLastNameMachani() throws Exception{ 
    final SAXBuilder saxbuilder = new SAXBuilder(); 
    final Document document = saxbuilder.build(new StringReader(xmlData)); 

    final XPathExpression<Element> compile = XPathFactory.instance().compile("//lastname[text()=\"machani\"]/../salary/basic", new ElementFilter()); 

    final Element machaniBaseSalary = compile.evaluateFirst(document); 

    assertThat(machaniBaseSalary.getText(), is("2000")); 
    machaniBaseSalary.setText("2100"); 
    assertThat(machaniBaseSalary.getText(), is("2100")); 
} 

}