2015-10-28 55 views
0

迴應是這樣的:如何用Java解析標籤從服務器

<oob> 
    <type>screen</type> 
    <value>idle</value> 
    <action>show</action> 
</oob> 
<oob> 
    <type>schedule</type> 
    <action>show</action> 
</oob> 

我希望把所有標籤爲鍵和值標記內的值。不知道標籤和標籤類型的數量。我想是這樣的:

//for first string from server 

public HashMap<String, String> response = new HashMap<String, String>(); 
response.put("type","screen"); 
response.put("value","idle"); 
response.put("action","show"); 

//for second string 

response.put("type","schedule"); 
response.put("action","show"); 

應該有邏輯來解析字符串:

if(server_response.contains("<oob>")){ 
    while(!endof server_response) 
    response.put("?","?"); 
} 

如何分析這種格式的服務器響應?

+0

(http://stackoverflow.com/questions/3906892/parse-an-xml-string-in-java) – javanut13

+2

的XML解析器。也許是舊的DOM,或許可以用XPath選擇它,也許可以使用JAXB,但基本上可以使用Java XML解析。 –

+0

http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ –

回答

0
import java.io.File; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.DocumentBuilder; 
import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.w3c.dom.Node; 
import org.w3c.dom.Element; 

public class DomParserDemo { 
    public static void main(String[] args){ 

     try { 
     File inputFile = new File("input.txt"); 
     DocumentBuilderFactory dbFactory 
      = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     Document doc = dBuilder.parse(inputFile); 
     doc.getDocumentElement().normalize(); 
     System.out.println("Root element :" 
      + doc.getDocumentElement().getNodeName()); 
     NodeList nList = doc.getElementsByTagName("student"); 
     System.out.println("----------------------------"); 
     for (int temp = 0; temp < nList.getLength(); temp++) { 
      Node nNode = nList.item(temp); 
      System.out.println("\nCurrent Element :" 
       + nNode.getNodeName()); 
      if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
       Element eElement = (Element) nNode; 
       System.out.println("Student roll no : " 
        + eElement.getAttribute("rollno")); 
       System.out.println("type : " 
        + eElement 
        .getElementsByTagName("type") 
        .item(0) 
        .getTextContent()); 
       System.out.println("value : " 
       + eElement 
        .getElementsByTagName("value") 
        .item(0) 
        .getTextContent()); 
       System.out.println("action: " 
       + eElement 
        .getElementsByTagName("action") 
        .item(0) 
        .getTextContent()); 

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

看看這個鏈接了。 http://www.tutorialspoint.com/java_xml/java_dom_parse_document.htm

+0

我們不知道標籤名稱本身。我想要標籤名稱和標籤值。此外,標籤的數量也不固定。 –

+0

好吧。罰款..看看這個太.http://stackoverflow.com/questions/24446849/parse-xml-without-tagname –

1

使用XML解析API,DOM API是最容易使用的一種,但您需要首先將字符串轉換爲文檔。

您可以使用循環將整個字符串轉換爲節點對象,您可以逐個檢查每個節點的預期元素並將其放到集合中。

下面是一些代碼示例,您可以嘗試:[谷歌一下]

DocumentBuilderFactory buildderfactory= DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db =buildderfactory.newDocumentBuilder(); 


    Document docXml = db.parse(new InputSource(new StringReader(yourxml))); 

    NodeList list = docXml.getElementsByTagName("oob"); 

    for (int i=0; i<list.getLength(); i++){ 

     System.out.println(i); 


     Node n = list.item(i); 
     Node child =n.getFirstChild(); 
     while(child!=null){ 

      System.out.println(child.getNodeName()); 
      System.out.println(child.getFirstChild().getNodeValue()); 
      child= child.getNextSibling(); 
     } 

    }