将xml dom解析为对象 [英] Parse xml dom to an object

查看:35
本文介绍了将xml dom解析为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用此类对象填充数组?

How can I fill an array with such 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>

找到了这样的表"但是接下来我该如何从行"中收集数据呢?

Found such "Table" but what i shoud do next to collect data from "Row"?

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

推荐答案

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

Try this one. I try to code the problem and get the following output. This code parses your XML and constructs the 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 + "]";
    }
    
}

输出:

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

输入:sam.xml

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

这篇关于将xml dom解析为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆