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>

我尝试将元素的值<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();

但我得到空值

4

1 回答 1

8

必须使用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());
}
于 2012-08-30T14:47:27.377 回答