我有xml文件
<?xml version="1.0" encoding="UTF-8"?>
<products xmlns="http://www.myapp.com/shop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.myapp.com/shop shop.xsd">
<category name="yyy">
<subcategory name="yyy1">
<goods name="yyy11">
<model>ferrari</model>
</goods>
</subcategory>
</category>
</products>
我尝试将元素<model>
的值设为
SAXBuilder builder = new SAXBuilder();
File xmlProductFile = new File("shop.xml");
Document document = builder.build(xmlProductFile);
Element rootNode = document.getRootElement();
String category = rootNode.getChild("model").getText();
但我得到空值
答案 0 :(得分:7)
您必须必须使用getDescendants(Filter<F>)
方法来选择特定的Element
Element root = document.getRootElement();
ElementFilter filter = new org.jdom2.filter.ElementFilter("model");
for(Element c : root.getDescendants(filter)) {
System.out.println(c.getTextNormalize());
}