2017-05-09 89 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(); 
           }