2017-05-09 127 views
-1

我有以下xml格式并且想要读取java中的元素。对XML解析非常新颖。具有子元素的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>

请帮助, 在此先感谢。

+0

使用带有JAXB的DOM和RegEx? – Aubin

+1

java有无数个xml解析器。在发布之前你有没有做过任何事情? – f1sh

+0

@ f1sh,是不同的过程。无法取得成功。 – zeet

回答

0

我不知道我理解你的问题,但是你的XML格式不正确(它应该以)结束。

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

例如:

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

UPDATE:

其实,XPathAPI类的是不是一个标准,但你可以使用XPath:

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

@zeet哦,是的:最后一行应该是,而不是

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