Java SAX ContentHandler to create new objects for every root node

Posted by behrk2 on Stack Overflow See other posts from Stack Overflow or by behrk2
Published on 2010-03-15T07:00:07Z Indexed on 2010/03/15 7:09 UTC
Read the original article Hit count: 459

Filed under:
|
|
|
|

Hello everyone,

I am using SAX to parse some XML. Let's say I have the following XML document:

<queue>
   <element A> 1 </element A>
   <element B> 2 </element B>
</queue>
<queue>
   <element A> 1 </element A>
   <element B> 2 </element B>
</queue>
<queue>
   <element A> 1 </element A>
   <element B> 2 </element B>
</queue>

And I also have an Elements class:

public static Elements {

  String element;

  public Elements() {

  }

  public void setElement(String element){
     this.element = element;
  }

  public String getElement(){
     return element;
  }

}

I am looking to write a ContentHandler that follows the following algorithm:

Vector v;
for every <queue> root node {
   Element element = new Element();

   for every <element> child node{
      element.setElement(value of current element);
   }
   v.addElement(element);
}

So, I want to create a bunch of Element objects and add each to a vector...with each Element object containing its own String values (from the child nodes found within the root nodes.

I know how to parse out the elements and all of those details, but can someone show me a sample of how to structure my ContentHandler to allow for the above algorithm?

Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml