包含子元素的{x 44}解析器

时间:2017-05-09 12:53:31

标签: java xml-parsing

我有以下xml格式,想要读取java中的元素。对于xml解析非常新。

<string xmlns="http://tempuri.org/">
<IDV><exshowroomPrice>48800</exshowroomPrice><idv_amount>46360</idv_amount><idv_amount_min>39406</idv_amount_min><idv_amount_max>53314</idv_amount_max><exshowroomPrice_min>41480</exshowroomPrice_min><exshowroomPrice_max>56120</exshowroomPrice_max><outputmessage></outputmessage></IDV>
<string>

我已添加此内容,之后无法提取元素。

     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                                        DocumentBuilder db = dbf.newDocumentBuilder();
                                        InputSource is = new InputSource();
                                        is.setCharacterStream(new StringReader(responsebuffer.toString()));
                                        Document doc = db.parse(is);
                                        doc.getDocumentElement().normalize();
                                        System.out.println(doc.getDocumentElement().getTextContent());



   NodeList nodes = doc.getChildNodes();
                                    Node no1 = (Node) nodes.item(0);
                                    if (doc.getDocumentElement().getChildNodes().getLength() > 0) {



                                            if (nodes.item(0).getNodeType() == Node.ELEMENT_NODE) {
                                                Element element = (Element) nodes.item(0);
                                                NodeList nl =element.getElementsByTagName("exshowroomPrice");

                                                 System.out.println(((Node)nl).getNodeValue());
                                            }


                                    }

O / P:<IDV><exshowroomPrice>48800</exshowroomPrice><idv_amount>46360</idv_amount><idv_amount_min>39406</idv_amount_min><idv_amount_max>53314</idv_amount_max><exshowroomPrice_min>41480</exshowroomPrice_min><exshowroomPrice_max>56120</exshowroomPrice_max><outputmessage></outputmessage></IDV>

请帮助, 提前谢谢。

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解您的问题,但您的XML格式不正确(应以此结束)。

也就是说,解析文档的代码是正确的,现在我认为提取单个元素的最简单方法是使用类XPathAPI。

例如:

        Node node = XPathAPI.selectSingleNode(doc, "//string/IDV/exshowroomPrice");
        System.out.println(node.getTextContent());

更新:

实际上,XPathAPI不是标准,但您可以使用XPath:

        XPath xpath = XPathFactory.newInstance().newXPath();
        String val = (String) xpath.evaluate("//string/IDV/exshowroomPrice/text()", doc, XPathConstants.STRING);
        System.out.println(val);

答案 1 :(得分:0)

终于得到了答案。

try {

                                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                                    DocumentBuilder db = dbf.newDocumentBuilder();
                                    InputSource is = new InputSource();
                                    is.setCharacterStream(new StringReader(responsebuffer.toString()));
                                    Document document = db.parse(is);
                                    document.getDocumentElement().normalize();
                                    //System.out.println(document.getDocumentElement().getTextContent());

                                    StringBuilder xmlStringBuilder = new StringBuilder();
                                    xmlStringBuilder.append("<?xml version=\"1.0\"?>");
                                    xmlStringBuilder.append(document.getDocumentElement().getTextContent());
                                    ByteArrayInputStream input = new ByteArrayInputStream(xmlStringBuilder.toString().getBytes("UTF-8"));
                                    Document doc = db.parse(input);

                                    NodeList nList = doc.getElementsByTagName("IDV");
                                    for (int temp = 0; temp < nList.getLength(); temp++) {
                                        Node nNode = nList.item(temp);
                                        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                                            Element eElement = (Element) nNode;


 System.out.println(eElement.getElementsByTagName("exshowroomPrice").item(0).getTextContent());
 System.out.println(eElement.getElementsByTagName("idv_amount").item(0).getTextContent());
  System.out.println(eElement.getElementsByTagName("idv_amount_min").item(0).getTextContent());
  System.out.println(eElement.getElementsByTagName("idv_amount_max").item(0).getTextContent());
 System.out.println(eElement.getElementsByTagName("exshowroomPrice_min").item(0).getTextContent());
 System.out.println(eElement.getElementsByTagName("exshowroomPrice_max").item(0).getTextContent());
 System.out.println(eElement.getElementsByTagName("outputmessage").item(0).getTextContent());




                                        }
                                    }

                                    //getElementsByTagName("exshowroomPrice")

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }