0
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body><getSessionId xmlns="http://treecorp.corp:8080/MEENH/service">
<multiRef xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xsi:type="apachesoap:Map">
<item>
    <key xsi:type="soapenc:string">Application</key>
    <value xsi:type="soapenc:string">MobileDevice</value>
</item>
<item>
    <key xsi:type="soapenc:string">Version</key>
    <value xsi:type="soapenc:string">1.0</value>
</item>
<item>
    <key xsi:type="soapenc:string">Username</key>
    <value xsi:type="soapenc:string">ramau</value>
</item>
<item>
    <key xsi:type="soapenc:string">token</key>
    <value xsi:type="soapenc:string"></value>
</item>
<item>
    <key xsi:type="soapenc:string">sessionID</key>
    <value xsi:type="soapenc:string">SESSIONID</value>
</item>
<item>
    <key xsi:type="soapenc:string">OSInformation</key>
    <value xsi:type="soapenc:string">windowsXP</value>
</item>
</multiRef>
</getSessionId>
</soap:Body>
</soap:Envelope>

This is my XML.

I want to parse this XML using Java and retreive a value based on the key . You can see in the XML there are lot of Key Value Pairs .

For Example if If I want to retrive value for Application I should get the value as MobileDevice. Like this kindly anyone help me to sort this issue.

Thanks in Advance.

4 Answers 4

2

There is another similar question.

I personally used XOM and liked it.

Sign up to request clarification or add additional context in comments.

Comments

0

Use the DOM, like shown here: http://www.java-samples.com/showtutorial.php?tutorialid=152 for example.

Comments

0

Since its a soap response, why don't you write a nice simple web service client to retrieve the response and automatically turn it into objects for you.

There's an example of that here.

Comments

0

You can use DSM stream parsing library to convert key-value pair to List then get what you want.

This is the first approach. To do this you should define your yaml mapping file as follows.

result:
  type: array     # result is array
  path: /.+item   # read item tag. the path is a regex.
  fields:
    key:
    value:

Java Code to parse XML:

DSM dsm=new DSMBuilder(new File("path/to/mapping.yaml")).setType(DSMBuilder.TYPE.XML).create();
List<Map<String, Object>> itemList =  (List<Map<String, Object>>)dsm.toObject(xmlFileContent);

Here is itemList list that converted to json.

[ {
  "key" : "Application",
  "value" : "MobileDevice"
}, {
  "key" : "Version",
  "value" : "1.0"
}, {
  "key" : "Username",
  "value" : "ramau"
}, {
  "key" : "token",
  "value" : null
}, {
  "key" : "sessionID",
  "value" : "SESSIONID"
}, {
  "key" : "OSInformation",
  "value" : "windowsXP"
} ]

The second approach is converting XML to a java map that it's key of map is value of key tag and value of the map is the value of the value tag in XML.

Here is Mapping File:

result:
  type: object     # result is map
  path: /.+multiRef # path is regex
  fields:
    key:
      path: item/key # read value of /.+multiRef/item/key tag
    value:
      path: item/value # read value of /.+multiRef/item/value tag
    application:
      parentPath: item  # assign default value when /.+multiRef/item tag is closed
      default: $self.data.value  # get value of value field.
      filter: $self.data.key=='Application' # assign if key filed is 'Application'
    version:
      parentPath: item
      default: $self.data.value
      filter: $self.data.key=='Version'
    username:
      parentPath: item
      default: $self.data.value
      filter: $self.data.key=='Username'
    token:
      parentPath: item
      default: $self.data.value
      filter: $self.data.key=='token'
    sessionID:
      parentPath: item
      default: $self.data.value
      filter: $self.data.key=='sessionID'
    OSInformation:
      parentPath: item
      default: $self.data.value
      filter: $self.data.key=='OSInformation'

Java Code to parse XML:

DSM dsm=new DSMBuilder(new File("path/to/mapping.yaml")).setType(DSMBuilder.TYPE.XML).create();
Map<String, Object> result =  (Map<String, Object>)dsm.toObject(xmlFileContent);

String application=result.get("application").toString()

Here is the JSON representation of Result:

{
  "key" : "OSInformation",
  "value" : "windowsXP",
  "application" : "MobileDevice",
  "version" : "1.0",
  "username" : "ramau",
  "token" : null,
  "sessionID" : "SESSIONID",
  "OSInformation" : "windowsXP"
}

In first it's come a bit complicated but if you look at docs it is very easy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.