将 xml dom 解析为对象

时间:2021-02-12 13:49:11

标签: java xml parsing dom

如何用这样的对象填充数组?

appBar: AppBar(
        centerTitle: true,
        title: Text(
          'data',
          textAlign: TextAlign.center,
        ),
        actions: [
          IconButton(
              icon: Icon(Icons.filter_sharp),
              onPressed: () {},
              alignment: Alignment(5, 0.0)),
          Directionality(
              textDirection: TextDirection.rtl,
              child: IconButton(
                  icon: Icon(Icons.arrow_back_ios), onPressed: () {})),
        ],
        leading: IconButton(icon: Icon(Icons.search), onPressed: () {}),
        backgroundColor: Theme.of(context).primaryColor,
      ),

来自xml结构:

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

找到了这样的“表格”,但接下来我应该怎么做才能从“行”中收集数据?

<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>

2 个答案:

答案 0 :(得分:1)

试试这个。我尝试对问题进行编码并获得以下输出。此代码解析您的 XML 并构造 Sam 对象。

代码:

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 + "]";
    }
    
}

输出:

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>

答案 1 :(得分:0)

你可以使用JAXB Unmarshalling

您还可以在 Examples JAXB Unmarshalling

中看到一些示例
相关问题