【问题标题】:Parsing Custom XML Response in Java using SAX使用 SAX 在 Java 中解析自定义 XML 响应
【发布时间】:2017-06-14 20:34:15
【问题描述】:

我正在尝试使用 SAX 解析类似于 xml 样式的响应。下面是我的响应格式。

    <Subscriber id="11005632326">
<info name="info1" value="12012012010"/>
<info name="info2" value="11005632326"/>
<info name="info3" value="12312321321"/>
<info name="info4" value="hJLDos"/>
<info name="info5" value="Apple A1778/Apple iPhone 7"/>
<info name="group" value="above"/>
<info name="language" value="en"/>
<info name="lastotatime" value=""/>
<info name="detected" value="2017-01-14 23:22:45.158365"/>
</Subscriber>

这里我试图获取标签中每个值的值,例如 info1.value、info2.value 等。

我试过下面的代码

File fXmlFile = new File("c:/temp/admresp.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder  dBuilder;
    try {
         dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName("info");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName()+"\n node value"+nNode.getNodeValue());
            nNode.getNodeValue();

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("info1: " + eElement.getAttribute("info1"));
            }
        }

    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

有人可以帮我吗,因为我在输出中的 info1 值中得到了空值

Current Element :info
node valuenull
info1 : 

【问题讨论】:

  • 你不是在使用 sax,你在使用 DOM 吗?

标签: java xml sax


【解决方案1】:

我相信 getElementsByTagName 方法只返回您调用它的元素正下方的元素。因此,您将希望在根元素而不是文档上使用它。您还需要注意空格/制表符/换行符等内容。

这种遍历文档的方式对我有用。

        Node child = doc.getDocumentElement().getFirstChild();
        while((child = child.getNextSibling()) != null) {
            if(child instanceof Element) {
                String name = ((Element) child).getAttribute("name");
                String value = ((Element) child).getAttribute("value");
                System.out.println("name: " + name + ", value: " + value);
            }
        }

【讨论】:

  • 感谢@steve,我在 docElement 上调用了不同的 getAttribute 方法并成功解决。
【解决方案2】:

使用下面的代码,我可以解析它,获取 tagName.Values 的属性,在我的情况下,它被插入到哈希图中。

//Using factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        //parse using builder to get DOM representation of the XML file
        dom=db.parse(new ByteArrayInputStream(xmlFile));
        Map<String,String> attrMap=new HashMap<String,String>();
        Element docEle = dom.getDocumentElement();
        //get a nodelist of      elements
        NodeList nl = docEle.getElementsByTagName("info");
        if(nl != null && nl.getLength() > 0) {
            for(int i = 0 ; i < nl.getLength();i++) {

                //get the info element
                Element el = (Element)nl.item(i);

                attrMap.put(el.getAttribute("name"), el.getAttribute("value"));
            }
        }

【讨论】:

    猜你喜欢
    • 2017-06-29
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多