Ray*_*Ray 2 java xml jdom xml-parsing jdom-2
我有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>
Run Code Online (Sandbox Code Playgroud)
我试图让元素的值<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();
Run Code Online (Sandbox Code Playgroud)
但我得到空值
您必须使用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());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9797 次 |
| 最近记录: |