[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Parsing XML with Same Node Names

Posted on 2004-09-08
2
Medium Priority
?
290 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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 video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

872 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