使用SAX解析Java中的自定义XML响应

时间:2017-06-14 20:34:16

标签: java xml sax

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

    <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值为null
Current Element :info
node valuenull
info1 : 

2 个答案:

答案 0 :(得分: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);
            }
        }

答案 1 :(得分:0)

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

//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"));
            }
        }