简体   繁体   English

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

[英]Parsing Custom XML Response in Java using SAX

I am trying to parse response similar to xml style using SAX.Below is my response format. 我正在尝试使用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>

Here I am trying to get the values of each value in the tag,like info1.value,info2.value,etc. 在这里,我试图获取标记中每个值的值,例如info1.value,info2.value等。

I have tried with following code 我尝试了以下代码

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();
    }

}

Can somone help me here as I am getting null in info1 value in the output 我在输出中获得的info1值为空时,可以在这里帮助我吗

Current Element :info
node valuenull
info1 : 

I believe the getElementsByTagName method only returns elements directly under the the element you call it on. 我相信getElementsByTagName方法只会在调用它的元素下直接返回元素。 So you will want to use it on the root element instead of document. 因此,您将需要在根元素而不是文档上使用它。 You will also need to be careful of things like whitespace/tabs/newlines. 您还需要注意空格/制表符/换行符。

This way of iterating through the document works for me. 这种遍历文档的方式对我有用。

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

With below code I am able to parse it getting the attribute of tagName.Values are inserting into a hashmap in my case. 通过下面的代码,我能够解析它来获取tagName的属性。在我的情况下,值将插入到哈希图中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM