Solved

Parsing XML with Same Node Names

Posted on 2004-09-08
2
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

690 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