• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Creating a MenuBar from Xml using Java

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just starting to learn xml and I am having the following problem. I am triying to parse an Xml file(in java) with the following structure, to create a Menubar:
<App>
<MenuBar NAME="TopMenu">
<Menu NAME="File" HANDLER="FileHandler">
<MenuItem NAME="FileOpen"LABEL="Open."/>
<MenuItem NAME="FileSave" LABEL="Save"/>
<MenuItem NAME="FileExit" LABEL="Exit"/>
</Menu>
<Menu NAME="Edit" HANDLER="EditHandler">
<MenuItem NAME="Undo" LABEL="Undo"/>
<MenuItem NAME="Cut" LABEL="Cut"/>
</Menu>
</MenuBar>
</App>
I am using apache parsers and trying to use XPATH statements as follows:
// find menu nodes
m_xpath = "//MenuBar/Menu";
NodeList MenuNodeList =
XPathAPI.selectNodeList(tmp_MenuBarNode, m_xpath);

for(int i=0; i < MenuNodeList.getLength(); i++)
{
System.out.println(
"Node Menu Value ----> " +i+ "= " + tmp_MenuNameList.item(i).getNodeValue());

/******* Below gets each menu item name ********/
xpath = "//MenuItem/@NAME";
NodeList MenuItemNameList = XPathAPI.selectNodeList(MenuNodeList.item(i), xpath);
for(int b=0; b< MenuItemNameList.getLength(); b++)
{
System.out.println("Node Item Value "+b+" = "+ MenuItemNameList.item(b).getNodeValue());
}

The code above Sytem.out's the Menu names correctly however it doesn't print the MenuItems correctly.
Any help would be greatly appreciated.
Thanks,
Stephen.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a hunch it is printing correctly. The
<MenuItem> nodes have no value.
Attribute, CDATASection, Comment,
ProcessingInstruction, and Text Nodes have values.
If you want to output the LABEL, you will need to
get it the way you got the NAME.
It looks like some of the Apache documentation
came straight from the W3C. The DOM Recommendation
deals with node values at
http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247
HTH,
Joe
 
Patrick ODonnell
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help
Cheers,
Patrick
 
rubbery bacon. rubbery tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic