[jdom-interest] parsing jdom
Chris B.
chris at tech.com.au
Sat Mar 6 04:05:18 PST 2004
List result = new ArrayList();
doit(doc.getRootElement(), "z", result);
void doit(Element el, String name, List result) {
if (el.getName().equals(name)) {
result.add(el.getParent());
}
Iterator it = el.getChildren().iterator();
while (it.hasNext()) {
doit((Element)it.next(), name, result);
}
}
Srinivas.Kotamraju at arvatosystems.com wrote:
> I am trying to parse an xml schema and create a new xml document . I
>am looking for an element which could be at any level inside an xml schema
>and return just the parents of that element using JDOM. I started the code
>but hit a dead end..Could some one help me with the code..
> for ex:
> <rootElement>
> <a></a>
> <b></b>
> <x>
> <y>
> <z></z>
> </y>
> </x>
> </rootElement>
> I have a method where I pass the element name z as an argument and I
>wish to get back the xml below(just the parents of the element I am looking
>for)
> ..note that elements a and b are skipped.
>
> <rootElement>
> <x>
> <y>
> <z></z>
> </y>
> </x>
> </rootElement>
>
>
> private void parseschema( String elementName, String schemafilename)
>throws IOException {
>
> boolean hasNoChildren=false;
> SAXBuilder builder = new SAXBuilder();
>
> try {
> Document schemaDoc = builder.build(schemafilename);
> List elements = schemaDoc.getRootElement().getChildren();
> if ( elements.size()==0 ) {
> hasNoChildren=true;
> //throw an exception.
> }
> else
> {
> Iterator iElts = elements.iterator();
> while (iElts.hasNext()) {
> Element currElt = (Element) iElts.next();
> String eleName = currElt .getName();
> if (eleName.equals(elementName))
> {
> //match
> }
> else
> {
> //continue descent until match and return only the parents of the
>element passed.
> }
> }
> }
>
> } catch (JDOMException e) {
> throw new IOException(e.getMessage());
> }
> }
>
>
> Thanks
> srini
>
>
>>
>>
>>
>_______________________________________________
>To control your jdom-interest membership:
>http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
>
>
More information about the jdom-interest
mailing list