簡體   English   中英

將 xml dom 解析為 object

[英]Parse xml dom to an object

如何用這樣的 object 填充數組?

class Sam{
String id;
String type;
String data;
}

來自 xml 結構:

<Table name="Sam">
      <Row id="374058">
         <Col name="ID.1">374058</Col>
         <Col name="TYPE.1">mob</Col>
      </Row>
      <Row id="374059">
         <Col name="ID.1">374059</Col>
         <Col name="TYPE.1">ff</Col>
      </Row>
   </Table>

找到了這樣的“表格”,但接下來我應該怎么做才能從“行”中收集數據?

List<Sam> samList = new new ArrayList<>();
NodeList nameNodeList = xml.getDocumentElement().getElementsByTagName("Table");
        if (nameNodeList != null) {
            for (int i = 0; i < nameNodeList.getLength(); i++) {
                Node node = nameNodeList.item(i);
                if (node.getAttributes().getNamedItem("name").getNodeValue().equals("Sam") &&
                        node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;

                   //what should i do next, how can i fined ID,TYPE,DATA?
                }
            }
        }

試試這個。 我嘗試對問題進行編碼並獲得以下 output。 此代碼解析您的 XML 並構造 Sam object。

代碼:

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.NodeList;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;

public class XmlParse {

    public static void main(String[] args) {
        XmlParse xmlParse = new XmlParse();
        xmlParse.xmlToObj();

    }

    public void xmlToObj() {

        try {
            
            File inputFile = new File("sam.xml");
            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 nodeList = doc.getElementsByTagName("Row");
            
            Node node = null;
            List<Sam> samList = new ArrayList<>();
            if(nodeList != null && nodeList.getLength() > 0) {
                for(int i=0; i < nodeList.getLength(); i++) {
                    node = nodeList.item(i);
                    NodeList innerNodeList =  doc.getElementsByTagName("Col");
                    
                    Node innerNodeID = innerNodeList.item(0);
                    Node innerNodeType = innerNodeList.item(1);
                    
                    String id =  innerNodeID.getTextContent();
                    String type = innerNodeType.getTextContent();
                    Sam sam = new Sam(id, type, null);
                    System.out.println(sam.toString());
                    
                    samList.add(sam);
                }
                
            }
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

class Sam {
    String id;
    String type;
    String data;
    public Sam(String id, String type, String data) {
        this.id = id;
        this.type = type;
        this.data = data;
    }
    @Override
    public String toString() {
        return "Sam [id=" + id + ", type=" + type + ", data=" + data + "]";
    }
    
}

Output:

Root element: Table
Sam [id=374058, type=mob, data=null]
Sam [id=374058, type=mob, data=null]

輸入:sam.xml

<Table name="Sam">
    <Row id="374058">
       <Col name="ID">374058</Col>
       <Col name="TYPE">mob</Col>
    </Row>
    <Row id="374059">
       <Col name="ID">374059</Col>
       <Col name="TYPE">ff</Col>
    </Row>
</Table>

您可以使用JAXB 解組

您還可以在示例 JAXB Unmarshalling中查看一些示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM