[jdom-interest] parsing jdom

Srinivas.Kotamraju at arvatosystems.com Srinivas.Kotamraju at arvatosystems.com
Fri Mar 5 09:35:48 PST 2004


	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
>  



More information about the jdom-interest mailing list