我尝试通过引用此链接在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>
我的员工标签不会有ID,而且我会有工资的子标签。 但我需要使用java在xml中更改sanjay(firstname)的工资。
任何建议都会有所帮助。
答案 0 :(得分: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());
}
}
}
答案 1 :(得分: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"));
}
}