如何解析java中的标签

时间:2015-10-28 03:59:56

标签: java string xml-parsing

来自服务器的响应是这样的:

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

如何以该格式解析服务器响应?

2 个答案:

答案 0 :(得分:1)

使用XML解析API,DOM API是最容易使用的API之一,但您需要先将字符串转换为Document。

您可以将整个字符串转换为Node对象,使用循环,您可以逐个检查每个(s)的预期元素并将其放入集合。

以下是您可以尝试的一些代码示例:

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

    }

答案 1 :(得分: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