0

What is the most efficient way to solve this problem: I've traversed a XML file and created the following set of linked (String) lists:

  • a > b > c
  • a > b > d
  • a > f > [i]

and now I'm trying to rebuild the XML into its original structure:

<a>
 <b>
  <c/><d/>
 </b>
 <f>i</f>
</a>

Any help would really be appreciated!

4 Answers 4

3

You probably don't want to use Lists as a data structure for this. You might be better off creating a Node type or something similar, which can contain text and child nodes, so that you can store the data in a tree / hierarchy of nodes. Something simple like this should do the trick:

public class Node {
    private String text;
    private List<Node> children = new ArrayList<Node>();

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public List<Node> getChildren() {
        return children;
    }

}

It should then be trivial to create a tree of these Nodes when you read in the file, and to use the same structure to write it back out.

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

Comments

0

You'll need to store more information about the structure of the original XML. Those 3 lists don't have information on the order of child nodes, for instance.

Comments

0

I'd use a tree data-structure to hold the elements in the first step (like matt explains here).

And your representation is unclear to me, how do you distinguish between a tag and an element? Since i is held in the same lists as a tag but isn't a tag.

Comments

0

A Set doesn't preserve document order. You want to keep track of the tree-structure in a range-based tabular encoding (pre/post/size/level/kind) or something along the lines, a prefix-based schema like ORDPATH or based on pointers as for instance a parent/leftsibl/rightsibl/firstchild/node-encoding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.