Solved

Parsing XML with Same Node Names

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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 …
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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