1

Here is my response contain XML file and I want to retrieve bEntityID="328" from this xml response

  <?xml version="1.0" encoding="UTF-8"?>
<ns2:aResponse xmlns:ns2="http://www.***.com/F1/F2/F3/2011-09-11">
   <createBEntityResponse bEntityID="328" />
</ns2:aResponse>

I am trying to this but getting null

    System.out.println("bEntitytID="+XmlPath.with(response.asString())
   .getInt("aResponse.createBEntityResponse.bEntityID"));

Any suggestion for getting BEntityID from this response?

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
MangduYogii
  • 935
  • 10
  • 24

3 Answers3

2

Though I dont suggest the below approach to use Regex to get element values, but if you are too desperate to get then try the below code:

public class xmlValue {
    public static void main(String[] args) {
        String xml = "<ns2:aResponse xmlns:ns2=\"http://www.***.com/F1/F2/F3/2011-09-11\">\n" +
                "   <createBEntityResponse bEntityID=\"328\" />\n" +
                "</ns2:aResponse>";
        System.out.println(getTagValue(xml,"createBEntityResponse bEntityID"));
    }
    public static  String  getTagValue(String xml, String tagName){
         String [] s;
         s = xml.split("createBEntityResponse bEntityID");
         String [] valuesBetweenQuotes = s[1].split("\"");
         return  valuesBetweenQuotes[1];
    }
    }

Output: 328

Note: Better solution is to use XML parsers

This will fetch the first tag value:


public static String getTagValue(String xml, String tagName){
    return xml.split("<"+tagName+">")[1].split("</"+tagName+">")[0];
}

Other way around is to use JSoup:

Document doc = Jsoup.parse(xml, "", Parser.xmlParser()); //parse the whole xml doc
for (Element e : doc.select("tagName")) {
    System.out.println(e); //select the specific tag and prints
}
Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
  • tried the first approach..but getting ArrayIndexOutOfBoundException – MangduYogii Aug 22 '19 at 12:04
  • @MangduYogii , Kindly check the first code that I added. Just wrap it in some function and it will always fetch you the value matching the tag. – Vishwa Ratna Aug 22 '19 at 13:53
  • This is very complicated way to parse the Xml , the code in question has a minor coding error if corrected will work. Not sure why a library changing approach is needed in this case. – Ashoka Aug 23 '19 at 06:41
0

I think the best way is deserializing xml to pojo like here, and then get value

entityResponse.getEntityId();
Genome
  • 15
  • 1
  • 7
0

I tried with the same XML file and was able to get the value of bEntityId with the following code. Hope it helps.

@Test
public void xmlPathTests() {
    try {
    File xmlExample = new File(System.getProperty("user.dir"), "src/test/resources/Data1.xml");
    String xmlContent = FileUtils.readFileToString(xmlExample);

    XmlPath xmlPath = new XmlPath(xmlContent).setRoot("aResponse");

    System.out.println(" Entity ::"+xmlPath.getInt(("createBEntityResponse.@bEntityID"))); 

    assertEquals(328, xmlPath.getInt(("createBEntityResponse.@bEntityID")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Ashoka
  • 935
  • 7
  • 20
  • What is the driver code for this Test case. What are Actual and Expected? – Vishwa Ratna Aug 22 '19 at 14:10
  • It's only for illustrative purpose, objective is to answer the user query, anyways have update the code snippet. Cheers for pointing it out. – Ashoka Aug 23 '19 at 06:34