-1

When I am printing my API response, which gives me below xml as Response:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <BugInfo xmlns="ctessng" xmlns:ns2="http://www.w3.org/1999/xlink">
   <Bug id="CSCvz53137">
    <Field name="Assigned Date">09/01/2021 21:12:25</Field>
    <Field name="Archived">N</Field>
    <Field name="Assigner">James Vilson</Field>
    <Field name="Status">V</Field>
    <Field name="Submitter">Spark Mery</Field>
    <Field name="Reason">Technically Inaccurate</Field>
    <Field name="Regression">Y</Field>
    <Field name="Resolved Date">09/02/2021 02:12:37</Field>
    <Field name="Version">001.010</Field>
   </Bug>
</BugInfo>

I want to fetch only specific values form this xml, like Assigned Date, Assigner, Submitter & Resolved-on

Assigned Date --> 09/01/2021 21:12:25
Assigner --> James Vilson
Submitter --> Spark Mery
Resolved Date --> 09/02/2021 02:12:37

What is the best/simplest way to read in values from this xml?

Garry S
  • 85
  • 1
  • 9
  • What framework/library do you use to send and receive the API request ? And did you try to solve it in Java already, any [example] you can show? – hc_dev Sep 22 '21 at 12:21
  • 1
    I am using OkHttpClient library for API request & tried multiple things like DocumentBuilderFactory & SAXParser – Garry S Sep 22 '21 at 12:38
  • OK, good start! Then please add some or all of these things you tried as code to your post. Then we have a concrete issue to solve. Such open questions for "best/simplest" are usually closed as [off-topic] and opinionated – hc_dev Sep 22 '21 at 12:46

1 Answers1

1

Regex

The most versatile would be plain text-filtering (match/find, extract) using a regular expression:

<Field name=\"(Assigned Date|Assigner|Submitter|Resolved Date)\">(.*)<

Iterating with find() then group(1) and group(2) can give you the desired strings.

See this regex demo

XPath

The pure XML-parsing way would be to use any XML parser, like DocumentBuilderFactory and SAXParser which can be used to read the XML into a document, then find the desired XML-nodes (Field elements) via XPath expression:

/BugInfo/Bug/Field[@name="Assigner"]|//Field[@name="Assigned Date"]|//Field[@name="Submitter"]|//Field[@name="Resolved Date"]

Iterating over the found nodes we can extract the child as text value.

XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xPath.compile(xPathExpression).evaluate(xmlDocument, XPathConstants.NODESET);

See:

XML mapping

The object-oriented way would use an XML mapper like Jackson to deserialize (unmarshall) the XML to an object. Similar to the OkHTTP Recipe: Parse a JSON Response With Moshi (.kt, .java)

Then you would need a class where you can map the XML nodes to.

class Bug {
    String submitter;
    String assigner;
    Date assignedOn;
    Date resolvedOn
}

The mapping can be a bit tricky here, because from XML-model point-of-view a Bug node contains a collection of children Fields. But the target type, is semantically not a field-list, but a Bug-object with different typed properties.

This is probably the cleanest because it will be easy to parse: Bug bug = new XmlMapper().readValue(xmlString, Bug.class).

hc_dev
  • 8,389
  • 1
  • 26
  • 38