File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes parsing in jdom Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "parsing in jdom" Watch "parsing in jdom" New topic
Author

parsing in jdom

srinivas krishnan
Greenhorn

Joined: Mar 03, 2004
Posts: 13
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

Joined: Mar 03, 2004
Posts: 13
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>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parsing in jdom