Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse XML Sequentially

Status
Not open for further replies.
Sep 5, 2006
37
0
0
US
Hi,

Need some help in parsing the following xml. I am trying build an hierarchical menu from the xml. Thr hierarchy can go to x-level deep.

Code:
<?xml version="1.0" encoding="windows-1252" ?>
<Menus>
  <Menu>
    <MenuID>1</MenuID>
    <MenuName>File</MenuName>
    <ParentID/>
    <NumChild>3</NumChild>
    <Menu>
      <MenuID>12</MenuID>
      <MenuName>Open</MenuName>
      <ParentID>1</ParentID>
      <NumChild>0</NumChild>
    </Menu>
    <Menu>
      <MenuID>11</MenuID>
      <MenuName>New</MenuName>
      <ParentID>1</ParentID>
      <NumChild>2</NumChild>
      <Menu>
        <MenuID>21</MenuID>
        <MenuName>Program</MenuName>
        <ParentID>11</ParentID>
        <NumChild>1</NumChild>
        <Menu>
          <MenuID>31</MenuID>
          <MenuName>Blank</MenuName>
          <ParentID>21</ParentID>
          <NumChild>0</NumChild>
        </Menu>
      </Menu>
      <Menu>
        <MenuID>61</MenuID>
        <MenuName>Program2</MenuName>
        <ParentID>11</ParentID>
        <NumChild>0</NumChild>
      </Menu>
    </Menu>
    <Menu>
      <MenuID>5</MenuID>
      <MenuName>Win</MenuName>
      <ParentID>1</ParentID>
      <NumChild>0</NumChild>
    </Menu>
  </Menu>
  <Menu>
    <MenuID>2</MenuID>
    <MenuName>View</MenuName>
    <ParentID/>
    <NumChild>1</NumChild>
    <Menu>
      <MenuID>22</MenuID>
      <MenuName>Printer</MenuName>
      <ParentID>2</ParentID>
      <NumChild>0</NumChild>
    </Menu>
  </Menu>
  <Menu>
    <MenuID>3</MenuID>
    <MenuName>Doc</MenuName>
    <ParentID/>
    <NumChild>0</NumChild>
  </Menu>
  <Menu>
    <MenuID>4</MenuID>
    <MenuName>Tools</MenuName>
    <ParentID/>
    <NumChild>0</NumChild>
  </Menu>
</Menus>

Any help is appreciated.

Thanks
 
Here is the sample out I am looking for:

Code:
<ul id="pcTLMgeneral" class="pcTLMenu">
     <li class="TLMopen">
      <a title="Design and development tools">Menu 1</a>
      <ul> 
       <li class="doc">       
       <a href="javascript:showData('<%=urlLink%>Sub Menu 1.1');">Sub Menu 1.1</a>
       </li>
      </ul>
     </li>
     <li class="TLMopen">
      <a title="On this site">Menu 2</a>
      <ul>
       <li class="doc">
        <a href="javascript:showData('<%=urlLink%>Sub Menu 2.1');">Sub Menu 2.1</a>
       </li>
       <li class="doc">
        <a href="javascript:showData('<%=urlLink%>Sub Menu 2.2');">Sub Menu 2.2</a>
       </li>
      </ul>
     </li>
     <li class="TLMopen">
      <a>Menu 4</a>
      <ul>
       <li class="TLMopen">
        <a>Sub Menu 4.1</a>
        <ul>
         <li class="doc">
          <a href="javascript:showData('<%=urlLink%>Sub Menu 4.1.1');">Sub Menu 4.1.1</a>
         </li>         
        </ul>
       </li>
      </ul>
     </li>
     <li class="TLMopen">
      <a title="Our opinions - mostly to do with information technology">Menu 5</a>
      <ul>
       <li class="doc">
        <a href="javascript:showData('<%=urlLink%>Sub Menu 5.1');">Sub Menu 5.1</a>
       </li>       
      </ul>
     </li>
     <li class="doc">
      <a href="javascript:showData('<%=urlLink%>Menu 6');">Menu 6</a>
     </li>
    </ul>

Here is the code so far I have but no luck:

Code:
    private void processElement(PrintWriter out, Element elem, 
                                int indent) // not used in this version
        throws Exception
    {
        NodeList children = elem.getChildNodes() ;
        for (int ii = 0 ; ii < children.getLength() ; ii++)
        {
            Node child = children.item(ii) ;
            if (child instanceof Element)
            {
                if ((child.getNodeType() == Node.ELEMENT_NODE) && (((Element)child).getTagName().equals("Menu")))
                {
                    Node menuId = ((Element)child).getElementsByTagName("MenuID").item(0) ;
                    Node child1 = menuId.getChildNodes().item(0) ;

                    Node menuName = ((Element)child).getElementsByTagName("MenuName").item(0) ;
                    Node child2 = menuName.getChildNodes().item(0) ;

                    Node parentId = ((Element)child).getElementsByTagName("ParentID").item(0) ;
                    Node child3 = parentId.getChildNodes().item(0) ;

                    Node numChild = ((Element)child).getElementsByTagName("NumChild").item(0) ;
                    Node child4 = numChild.getChildNodes().item(0) ;

                    if (child3 == null && Integer.parseInt(child4.getNodeValue()) > 0)
                    {
                        menu += "<li class=\"TLMopen\"><a>" + child2.getNodeValue() + "</a>" ;
                    }

                    if (child3 == null && Integer.parseInt(child4.getNodeValue()) == 0)
                    {
                        menu += "<li class=\"doc\"><a href=\"javascript:showData('Menu 6');\">" + child2.getNodeValue() + "</a></li>" ;
                    }

                    if (child3 != null && Integer.parseInt(child4.getNodeValue()) == 0)
                    {
                        menu += "<ul><li class=\"doc\"><a href=\"javascript:showData('Sub Menu 1.1');\">" + child2.getNodeValue() + "</a></li></ul></li>" ;
                    }

                    if (child3 != null && Integer.parseInt(child4.getNodeValue()) > 0)
                    {
                        menu += "<li class=\"TLMopen\"><a>"+child2.getNodeValue()+"</a><ul>" ;
                    }
                }
                processElement(out, (Element)child, indent + 4) ;
            }
            else if (child instanceof Text)
            {
                ; //out.println("Text: " + child.getNodeValue()) ;
            }
        }

        out.flush() ;
    }

Thanks
 
So what kind problems are you facing? Parsing problems? Value problems? Exceptions?

Cheers,
Dian
 
Hi,

The final output is not coming correctly as am not able to keep track of the hierarchy of menus. As <Menu> may contain multiple <Menu> tags i.e. hierarchy. So am facing problem on final output not coming up right. As in the above sample xml I need the final output like follows:

Code:
File
    Open
    New
       Program
              Blank
       Program2
    Win
View
    Printer
Doc
Tools

As File contain multiple sub-menus that expand collapse etc. Any help is appreciated.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top