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

Using NodeList

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working with this code, and don't know if how it's done is the best way.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document XMLDoc = factory.newDocumentBuilder().parse(new URL(xmlFile).openStream());
NodeList linkNames = XMLDoc.getElementsByTagName("urlName");
for (int i=0; i < linkNames.getLength(); i++) {

Node curNode = linkNames.item(i);
NamedNodeMap attrs = curNode.getAttributes();

String currTag = curNode.getNodeName();

if(currTag.equals("urlName"))
urlName.add(i, linkNames.item(i).toString());

}
The xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<link>
<urlName>whats new?</urlName>
<urlPath>getcurrent.jsp?page=whatsnew</urlPath>

<subLink>
<subUrlName>announcements</subUrlName>
<subUrlPath/>
</subLink>

</link>
</menu>
Any help would be great thanks.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't think of anything faster or cleaner as long as you are working with the DOM.
Bill
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill,
I heard people saying that JDOM simplifies a lot of things. I don't know exactly what they mean by that.
Just curious.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not delved too deeply into JDOM but I have also heard that it greatly simplifies working with XML because the structures are closer to normal Java practice. There is a chance that JDOM may become an official alternate to JAXP as I understand it.
Bill
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jdom is much faster than dom, and huge more flexible than sax. Easy to use. It uses sax internally, build the tree By using lazy initialization design pattern. It completes more than 80% work of dom with only 20% of the effort. Since it can use xpath/transformer etc. It actually can do more than dom now.
In XML, I am a native speaker of jdom. I learned everything else after jdom. I also introduced jdom to my current work space.
 
That new kid is a freak. Show him this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic