• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Question on JDOM

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i just downloaded JDOM and i have to say i find it much easier to learn than the xerces Api.
My Question is : Is there a possebility to search the (whole!)Document for a specified childnode ? . As Result i want a list of all Childs in the document with the given name.
I didnt found such a method - all methods i found
only find child elements nested directly (one level deep).
 
Holger Prause
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Sorry i accidentially pressed Return and submitted the topic- *gg*

So my conclusion is you have always to be aware of the XML Spezifikation of the Document you have.You must browse the tree until you reach the node with the child you want to have.
Right ?

Thx,
Holger
[This message has been edited by Holger Prause (edited March 29, 2001).]
 
Holger Prause
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I think i found a reasonalbe solution - ill test how it works on huge Documents
here it comes - ill just show the important methods
<pre>
public Main() {
try {
// Build w/ SAX and Xerces, no validation
SAXBuilder b = new SAXBuilder();
// Create the document
doc = b.build(new File(xmlpath));
Element rootElement = doc.getRootElement();
Vector myChilds = getAllElementsWithName(rootElement,"TextElement");
log("Vector myChilds contains " + myChilds.size()+ " elements");
// Output as XML to screen
//XMLOutputter outputter = new XMLOutputter();
//outputter.output(doc, System.out);
} catch (Exception e)
{
log(e.getMessage());
e.printStackTrace();
}
}
//this method returns a Vector of all Elements with the specified name
private Vector getAllElementsWithName(Element elem,String elemName)
throws Exception{
Vector childVector = new Vector(20,1);
findChilds(childVector,elem,elemName);
return childVector;
}
//this method works recursivly through the DOM - putting all Elements in
//the spezicied Vector
private void findChilds(Vector childContainer,Element elem,String elemName)
throws Exception {
List childs = elem.getChildren();
for(int i = 0;i <childs.size();i++) {
Element tmpelem = (Element)childs.get(i);
log("processing node: "+tmpelem.getName());
if(tmpelem.getName().equals(elemName)) {
childContainer.addElement(tmpelem);
log("Element "+elemName+ "found");
}
else {
findChilds(childContainer,tmpelem, elemName);
}
}
}
</pre>
thats it - i like JDOM
[This message has been edited by Holger Prause (edited March 29, 2001).]
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holger, I�ve heard about werken.xpath, which is said to implement xpath on top of jdom. �werken.xpath takes as input, a XPath expression, and a JDOM tree, and returns a NodeSet (java.util.List) of selected elements� � that�s from their web site: http://code.werken.com/xpath/
 
Holger Prause
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>Holger, I�ve heard about werken.xpath, which is said to >implement xpath on top of jdom. �werken.xpath takes as input, a >XPath expression, and a JDOM tree, and returns a NodeSet
>(java.util.List) of selected elements

Hi,
thx for this tip.
I think i am quite familiar with XSLT+XPath so ill take a look at it.In the Xalan Api, this is also possible.(www.apache.org)

Bye,
Holger
 
Popeye has his spinach. I have this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic