cancel
Showing results for 
Search instead for 
Did you mean: 

Search a attribute value in a xml formated string ?

Former Member
0 Kudos

Hello everyone ,

I have a xml formated string . the inhalt is,

String xml = "


<version-data>
  <version-label/>
  <payload type="BF">
       <businessForm id="BFQuestion 1" BO="CAG_QUESTIONNAIRE"    guid="25057326DBA9B14DA7D1B145B4E97E2D" timestamp="2007-02-21T09:30:05.4210000" >
           <text spras="EN" text="aaa"/>
             ................................
 
      </businessForm>
  </payload>
</version-data>

";

i want to find the value of a attribute in this String.

e.g. guid="25057326DBA9B14DA7D1B145B4E97E2D"

i wanna get the value 25057326DBA9B14DA7D1B145B4E97E2D of attribute guid.

how can i do?

Thx in advance.

Regards.

Yaning

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

You can parse this XML string using either the DOM/SAX APIs. The code looks like this:


DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document doc = documentBuilder.parse(new InputSource(new StringReader(XMLString)));
NodeList nlist=doc.getElementsByTagName("businessForm");
String attrVal = nlist.item(0).getAttributes().getNamedItem("guid").getNodeValue();

Regards,

Satyajit.

Former Member
0 Kudos

The simplest way:

int i = xml.indexOf("guid=\"");

if (i >= 0) {

int j = xml.indexOf("\"", i + "guid=\"".length());

if (j >= 0) {

String result = xml.substring(i, j);

}

}

Former Member
0 Kudos

Hi,

You can use DOM parser or Pattern class.

In case of Pattern Class, split function should help.

For DOM Parser,something like this

var parser = new DOMParser();

var doc = parser.parseFromString("<items><item name='Apple'/><items>", "text/xml");

Once parsed, you can read elements ,attributes etc.,

Try this : <a href="http://www.xulplanet.com/tutorials/mozsdk/xmlparse.php">Parse Help</a>

Regards

Bharathwaj