[英]get value of element in xml-file using JDOM
我有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();
但是我得到空值
您必須使用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());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.