• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Parse XML return to java array

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I have a Rest webservice which returns in xml and store in my local variable as <root><Details><action>successful</action><licenceDetail><licence>SA123</licence><type>SZ Abalone</type><Vessel><name>Red Fire</name><number>SA123</number></Vessel><defaultvalue><LandingPort>Anxious Bay</LandingPort><DepartPort>Acramans Creek</DepartPort><Vessel>SA123</Vessel><AreaFishing>SA</AreaFishing></defaultvalue></licenceDetail><licenceDetail><licence>K01</licence><type>Blue Crab</type><Vessel><name>Abrolhos Spirit</name><number>K01</number></Vessel><defaultvalue /></licenceDetail></Details></root>

Now, I wanted each XML tag value in a local variable and use java array where ever is possible.

Regards,
RD
 
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will not give you a complete solution here but you should check the package org.wc3.dom which is part of standard JDK as far as I know.

You can convert a string with xml content into a document like this

Considering that "xmlString" is your xml string. The code is obviously very compressed (you'll probably want to split it up to multiple lines).

You can find documentation of package org.wc3.dom here. The Document interface is documented here.

You'll find plenty of examples online.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
That question is too difficult for the “beginning” forum, so I shall move you to our XML forum.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a few options for unmarshalling XML into Java objects. The commonest one (I think) is JAXB, although there are others.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:There are a few options for unmarshalling XML into Java objects. The commonest one (I think) is JAXB, although there are others.



One of the others is the Apache Digester. Originally it was part of one of the Apache products, but these days it's used in so many projects that it's a product in its own right.

JAXB is more or less intended to convert XML to its exact equivalent in Java classes. This is useful, and one thing I've done with it in fact, is double-annotate model objects so that the same class serializes both as XML and as a database (JPA) model. Saves a lot of extra coding and parallel maintenance.

However, sometimes your intended Java objects aren't quite so exact a mapping, and this is where the Digester is popular.
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic