1

我已经尝试通过引用此链接在 jdom 的帮助下更改 xml 值 - http://www.mkyong.com/java/how-to-modify-xml-file-in-java-jdom/

示例中显示的 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>

我的员工标签不会有身份证,我也会有工资标签。但我需要使用 java 在 xml 中更改 sanjay(firstname) 的薪水。

任何的意见都将会有帮助。

4

2 回答 2

5

如果您使用的是 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());
        }
    }

}
于 2012-12-13T22:46:08.880 回答
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"));
}

}

于 2012-11-27T15:33:54.973 回答