Solved

Parsing XML with Same Node Names

Posted on 2004-09-08
2
276 Views
Last Modified: 2012-06-27
I have an xml file in the form of:

<sample>
    <input>
        <fieild-name>Field Name 1</field-name>
    </input>
    <result>
        <fieild-name>Field Name</field-name>
    </result>
</sample>

How do I parse this xml file but still know what parent node field-name came from being either input or result.  I am using a SaxParser in java.  Thanks.
0
Comment
Question by:paulca
2 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 50 total points
ID: 12009956
You need to keep state.  When you hit the input element, set a state variable to some value (like 1) and when you hit the result element, set the state variable to another value (like 2).  Now when you hit a 'fieild-name' element you can look at the state variable to see whether you are currently in an input or result element.  You might set the state back to 0 upon leaving the input or result element.
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 12013350
it's rather easy :)

you can use stack to remember name of currently processing element.
when you recieve  elementStart() - push element name on stack. when you recieve elementEnd - pop last one element name from stack.

you can take a look at Apache Digester project - it uses this approach and add Rule processing engine - so may be you find it useful, and stop using SAX API directly.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now