【问题标题】:How to parse tags in java如何在java中解析标签
【发布时间】:2016-01-27 17:35:06
【问题描述】:

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

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

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

【问题讨论】:

标签: java string xml-parsing


【解决方案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();
        }

    }

【讨论】:

    【解决方案2】:
    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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2020-05-19
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    相关资源
    最近更新 更多