Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4026550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:59:03+00:00 2026-05-20T10:59:03+00:00

I have a problem when I parse the xml. The xml format is like

  • 0

I have a problem when I parse the xml.
The xml format is like this,

<root>
   <menu>
       <items>
           <menu>
               <items>               
                   <menu>
                       <content/>
                   </menu>
                   <menu>
                       <content/>
                   </menu>
                   <menu>
                       <content/>
                   </menu>
               <items>               
           </menu>
           <menu>
               <items>               
                   <menu>
                       <content/>
                   </menu>
                   <menu>
                       <content/>
                   </menu>
                   <menu>
                      <items>               
                         <menu>
                             <content/>
                         </menu>
                         <menu>
                             <content/>
                         </menu>
                         <menu>
                             <content/>
                         </menu>
                      <items>
                   </menu>
               <items>
           </menu>
       </items>
   </menu>
</root>

I do not know how deep about the xml……
Can I use two sax parser to parse this xml while I read is , call the MenuItemParser to parse, while I read is , call the ItemsParser to parse???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:59:03+00:00Added an answer on May 20, 2026 at 10:59 am

    Yep, it can be done. I’ll assume you’re using the org.xml.sax implementation (but the basic technique should work with any SAX-type parser).

    When using SAX you have an XMLParser that performs the actual parsing, and you pass to it an object implementing (for example) the ContentHandler interface.

    If you want to handle the items separately to the menus, what you do is use two ContentHandlers, say ItemContentHandler and MenuContentHandler. In each handler, if you run into content you want handled by the other handler, you simply tell the XMLReader to use the other handler instead.

    If you want everything contained in <menu> tags to be handled by the MenuContentHandler, and everything inside <items> tags to be handled by the ItemContentHandler, you would do something like the following:

    class MenuContentHandler implements ContentHandler
    {
        public XMLReader reader;
        public ItemContentHandler itemHandler;
    
        ...
        public void startElement(java.lang.String uri, java.lang.String localName,
            java.lang.String qName, Attributes atts)
        {
            if (localName.equals("items"))
                reader.setContentHandler(itemHandler); // Point 1
        }
        ...
        public void endElement(java.lang.String uri, java.lang.String localName,
            java.lang.String qName)
        {
            if (localName.equals("menu"))
                reader.setContentHandler(itemHandler); // Point 2
        }
        ...
    }
    
    class ItemContentHandler implements ContentHandler
    {
        public XMLReader reader;
        public MenuContentHandler menuHandler;
    
        ...
        public void startElement(java.lang.String uri, java.lang.String localName,
            java.lang.String qName, Attributes atts)
        {
            if (localName.equals("menu"))
                reader.setContentHandler(menuHandler); // Point 3
        }
        ...
        public void endElement(java.lang.String uri, java.lang.String localName,
            java.lang.String qName)
        {
            if (localName.equals("items"))
                reader.setContentHandler(menuHandler); // Point 4
        }
        ...
    }
    ...
    void doParsing ( )
    {
        XMLReader reader = XMLReaderFactory.createXMLReader();
        MenuContentHandler menuHandler = new MenuContentHandler(reader);
        ItemContentHandler itemHandler = new ItemContentHandler(reader);
    
        menuHandler.itemHandler = itemHandler;
        itemHandler.menuHandler = menuHandler;
    
        reader.setContentHandler(menuhandler);
        reader.parse (/*your document*/);
    }
    

    Not the best code in the world, but hopefully it gets the point across… If you need more, just let me know.

    EDIT: how this works – imagine the following snippet of XML:

     1    <menu>
     2        <items>
     3            <menu>
     4                <content/>
     5            </menu>
     6            <menu>
     7                <content/>
     8            </menu>
     9            <menu>
    10                <content/>
    11            </menu>
    12        </items>
    13    </menu>
    

    Assume that when the reader starts with this snippet, the ItemContentHandler is in control.

    The first thing it encounters is the <menu> tag on line 1. This indicates the start of a menu item, so we switch to the MenuContentHandler (this is marked “Point 3” above) so that we can read the contents of the menu element.

    In this case, the first thing in the element is actually an item element (line 2), so in the same way we change to the ItemContentHandler so that it can handle the contents of the item element (this time, at point 1).

    Line 3 is a repeat of line 1, so again we switch to the MenuContentHandler at point 3 to examine the content of the menu element.

    The next element is the <content/> tag on line 4, which is handled by the MenuContentHandler (which, I mentioned in the last paragraph, is currently in charge).

    On line 5, the menu closes with a </menu> tag. Now, since all the menu elements are contained within item elements, we know that we must now be in the menu’s containing item element. Therefore, we switch to the ItemContentHandler. This is point 2.

    Line 6 starts a new menu item, and so is treated the same way as lines 1 and 3. And so on for lines 7 through 11.

    Line 12 ends the items element, and by the equivalent logic as for lines 5, 8 and 11, we know that we must now be in the menu element that contains the item element. So, we change into the MenuContentHandler (point 4).

    Line 12 is the end of a menu item, and so is treated the same as lines 5, 8 and 11.

    Hope that explains it a bit better!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML structure that looks like this: <root> <index> <item>item 1</item> <item>item
I have a problem with using the SAX parser to parse a XML file.
I have a problem with this code right here: - (void)fetchedData:(NSData *)responseData { //parse
I have an xml which contains some keywords. The format of the xml this:
I have a problem right now, I need to parse an XML but some
I'm using JDOM with SAXBuilder to parse XML files, and I have a problem
I have a problem which requires me to parse several log files from a
I have a problem with sax parser and encoded text. I try to parse
I have a problem with parseInt() , it throws the error: unable to parse
I have one interesting problem. I must parse mail body (regular expression), get some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.