Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XML Parsing with Java - Only some attributes have start and end tags

843834Jul 9 2009 — edited Jul 10 2009
Hello,

I am trying to interpret the XML response from a server after a HTTP request is sent to it. I have modified an example off Wikipedia below to give an example of what it looks like.

Example XML:

<Asset name="bread" prep_time="5 mins" cook_time="3 hours">
<Attribute name="Ingredient: ">Flour</Attribute>
<Attribute name="Ingredient: ">Water</Attribute>
<Attribute name="Used in Recipes:" />
</recipe>

I have been trying to display the above XML in a format such as:

name: bread prep_time: 5 mins cook_time: 3 hours
Ingredient: Flour
Ingredient: Water
Used in Recipes:

Right now I have been trying to do it manually using indexOf and substring of the XML text, but I would like to find a better way of doing this. I have found some examples online that show had to deal with cases such as

<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>

but I do not know what to do when I encounter something like the last item, when there are no "Used in Recipe" items, and it ends with a />, instead of an ending tag </Attribute>.

Are there any suggestions as to how to handle the example XML code that I have posted?

Any help would be appreciated. Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 7 2009
Added on Jul 9 2009
11 comments
265 views