1

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.

1
  • I haven't used jaxb for years but would it work here? Commented Dec 9, 2011 at 20:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I am trying to deserialize, so it should be xs.fromXml()...however I am getting someother exceptions, which I am trying to resolve...Thanks!!
This code also works for deserialize. Just use fromXML and it will return your object. I'll add the deserialize code to the answer.
this is what i get Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at com.thoughtworks.xstream.core.util.FastStack.pop(FastStack.java:42) at com.thoughtworks.xstream.io.xml.AbstractPullReader.move(AbstractPullReader.java:125) at com.thoughtworks.xstream.io.xml.AbstractPullReader.moveDown(AbstractPullReader.java:103) at com.thoughtworks.xstream.io.xml.XppReader.<init>(XppReader.java:63) ------------------------------------------ trying to figure out the problem
ok...so there was some problem in reading the xml.... your solution works...just that it's not reading from an xml file for me...but that should be relatively easy to fix... Thanks!!!
btw..do you know how to do the same thing using apache digester?? i guess if xstream supports it, digeser would also be able to...
0
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); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.