简体   繁体   中英

parsing xml containing nested tags

xml:

<Node name="1">
   <Node name="2">
       <Node name="4"></Node>
   </Node>
   <Node name = "3">
       <Node name="5"></Node>
    </Node>
</Node>

I want to create the following object in java

Node{
 String name;
 List<Node> nodeList
}

Is there any xml parsing library which could do this. I have tried xstream and simple, but have not been able to figure it out.

Any help would be appreciated.

This code uses XStream and generates the output you are looking for.

The node class:

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

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("Node")
public class Node {
    @XStreamAsAttribute
    private String name;

    @XStreamImplicit
    private List<Node> nodes = new ArrayList<Node>();

    public Node(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Node> getNodes() {
        return nodes;
    }

    public void setNodes(List<Node> nodes) {
        this.nodes = nodes;
    }

    public void addNode(Node n) {
        nodes.add(n);
    }
}

The Main class:

import com.thoughtworks.xstream.XStream;


public class NodeXStream {

    public static void main(String[] args) {
        Node n1 = new Node("1");
        Node n2 = new Node("2");
        Node n3 = new Node("3");
        Node n4 = new Node("4");
        Node n5 = new Node("5");

        n1.addNode(n2);
        n1.addNode(n3);

        n2.addNode(n4);
        n3.addNode(n5);


        XStream xs = new XStream();
        xs.processAnnotations(Node.class);

        // To XML
        String myXML = xs.toXML(n1);

        // From XML
        Node newNode = (Node) xs.fromXML(myXML);

    }

}

EDIT: Added the deserialization code.

To deserialize you'll also need to add the XPP3 library to the build path. It is part of XStream.

public class DigestNodes {
    List<Node> nodes;

    public DigestNodes() {
        nodes= new ArrayList<Node>();
    }

    public static void main(String[] args) {
        DigestNodes digestStudents = new DigestNodes();
        digestStudents.digest();
    }

    private void digest() {
        try {
            Digester digester = new Digester();

            digester.push(this);

            digester.addObjectCreate("*/node", Node.class );

            digester.addSetProperties( "*/node");

            digester.addSetNext( "*/node", "addNode" );

            DigestNodes ds = (DigestNodes) digester.parse(this.getClass()
                                .getClassLoader()
                                .getResourceAsStream("node.xml"));

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void addNode( Node node ) {
        nodes.add(node);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM