[jdom-interest] parsing jdom

Chris B. chris at tech.com.au
Mon Mar 8 14:52:17 PST 2004


So if I understand you correctly, you want to remove any element that 
doesn't have "z" as either a direct or indirect child.

I havn't tested this, but I think it should be close to what you want.

doit(doc.getRootElement(), "z");

void doit(Element el, String name) {
    boolean found = el.getName().equals(name);
    Iterator it = el.getChildren().iterator();
    while (it.hasNext()) {
       found = found || doit((Element)it.next(), name);
    }
    if (found) {
       return true;
    } else {
       if (el.getParent() != null) {
	   el.getParent().remove(el);
       }
       return false;
    }
}




Srinivas.Kotamraju at arvatosystems.com wrote:

>Hi Chris,
>Thanks for the response.But how would I be able to generate the new document
>with the List?In the method doit() I get  a list of elements, as a List. How
>would I iterate the List to get back the new structure having the integrity
>with regards to the depth level?
>regards
>
> 
>-----Original Message-----
>From: Chris B. [mailto:chris at tech.com.au]
>Sent: Saturday, March 06, 2004 7:05 AM
>To: Srinivas.Kotamraju at arvatosystems.com
>Cc: jdom-interest at jdom.org
>Subject: Re: [jdom-interest] parsing jdom
>
>
>
>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@yourho
>>    
>>
>st.com
>  
>
>> 
>>
>>    
>>



More information about the jdom-interest mailing list