Home > Net >  How to extract string from this xml format?
How to extract string from this xml format?

Time:06-24

I want to extract serial number from given xml string ?

<?xml version="1.0" encoding="UTF-8" standalone="yes"? ><Deviceinfo dc="21837d56-6153-498c-b344-74b93b3fff7b" dpld="Morpho.SmartChip"mc="MIIEHDCCAwSgAwIBAgIGAYGUOnxxMA0GCSqGSIb3DQEBCWUAM IG4MSEWHWYDV<additional_info> <Param name="serial_number" value="22061002574"/> <Param name="srno" value="22061002574"/> </additional_info></DeviceInfo><?xml version="1.0" encoding="UTF-8" standalone="yes"?><RDService info="Ready to use" status="READY"> <Interface id="CAPTURE" path="in.gov.uidai.rdservice.fp.CAPTURE"/> <Interface id="DEVICEINFO" path="in.gov.uidai.rdservice.fp.INFO"/></RDService>

CodePudding user response:

There are various options. Depending on the use case

  1. You can use xpath
  2. Use regex by passing the XML as a string
  3. Use binding mechanisms such as JAXB
  4. Use parsers such as DOM or SAX

CodePudding user response:

Create a DTO(e.g XmlDto) for attributes in this XML structure and use fromXML(<XML String>) method of XStream class available in java. Then you may use like this :

String request = "XML input string";
private XStream xstream;
XmlDto fromXML = (XmlDto) xstream.fromXML(request);
return fromXML.getXmlDto().<get your attribute w.r.t structure defined>;
  • Related