Solved

Parsing XML with Same Node Names

Posted on 2004-09-08
2
277 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

14 Experts available now in Live!

Get 1:1 Help Now