JDOM getChildren()返回空列表

时间:2012-09-25 11:29:59

标签: java xml jdom

这是我的xml:

示例:

<?xml version="1.0" encoding="UTF_8" standalone="yes"?>
<StoreMessage xmlns="http://www.xxx.com/feed">
    <billingDetail>
        <billingDetailId>987</billingDetailId>       
        <contextId>0</contextId>
        <userId>
            <pan>F0F8DJH348DJ</pan>
            <contractSerialNumber>46446</contractSerialNumber>            
        </userId>            
        <declaredVehicleClass>A</declaredVehicleClass>
    </billingDetail>
    <billingDetail>
        <billingDetailId>543</billingDetailId>       
        <contextId>0</contextId>
        <userId>
            <pan>F0F854534534348DJ</pan>
            <contractSerialNumber>4666546446</contractSerialNumber>            
        </userId>            
        <declaredVehicleClass>C</declaredVehicleClass>
    </billingDetail>
</StoreMessage>

使用JDOM解析器,我希望从中获取所有<billingDetail>个xml节点。

我的代码:

SAXBuilder builder = new SAXBuilder();
try {
    Reader in = new StringReader(xmlAsString);
    Document document = (Document)builder.build(in);
    Element rootNode = document.getRootElement();
    List<?> list = rootNode.getChildren("billingDetail");

    XMLOutputter outp = new XMLOutputter();
        outp.setFormat(Format.getCompactFormat());

        for (int i = 0; i < list.size(); i++) {

            Element node = (Element)list.get(i);

            StringWriter sw = new StringWriter();
            outp.output(node.getContent(), sw);
            StringBuffer sb = sw.getBuffer();

            String text = sb.toString();
            xmlRecords.add(sb.toString());
        }

} catch (IOException io) {
    io.printStackTrace();
} catch (JDOMException jdomex) {
    jdomex.printStackTrace();
}

但我永远不会像字符串那样输出xml节点:

<billingDetail>
    <billingDetailId>987</billingDetailId>       
    <contextId>0</contextId>
    <userId>
        <pan>F0F8DJH348DJ</pan>
        <contractSerialNumber>46446</contractSerialNumber>            
    </userId>            
    <declaredVehicleClass>A</declaredVehicleClass>
</billingDetail>

我做错了什么?如何使用JDOM解析器获得此输出?

修改

为什么如果XML以

开头

<StoreMessage>改为<StoreMessage xmlns="http://www.xxx.com/MediationFeed">

那么有效吗?这怎么可能?

0 个答案:

没有答案