Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

parsing in jdom

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to parse 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 could not finish it..Could some one help me with the code..
for ex:
<rootElement>
<a>
<b>
<x>
<y>
<z></z>
</y>
</x>
</b>
</a>
</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());
}
}
 
srinivas krishnan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the ex I mentioned is not right..here is the sample xml
<rootElement>
<a></a>
<b></b>
<x>
<y>
<z></z>
</y>
</x>
</rootElement>
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic