cancel
Showing results for 
Search instead for 
Did you mean: 

Read XMLString - urgent

GSL_NARAYANA
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

I have a string which holds a valid XML

some thing like this

strXML = "<GSP ver='3.2'><tm>7.622</tm><RES SN='1'><M>126000</M></RES></GSP>";

Can any one tell me how to get the value of "126000" from the above XMLstring in EP.

Note: I want to use DOM parser to get the value.

Thanks in advance

Lakshminarayana

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi, if you are shure there is just one Element named "M" you can also read the value this way:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setNamespaceAware(true);DocumentBuilder

docbuilder = null;

try

{

docbuilder = factory.newDocumentBuilder();

}

catch (ParserConfigurationException pce){pce.printStackTrace();}

try

{

Document XML_in = docbuilder.parse(XMLStr);

if (XML_in.getElementsByTagName("M").item(0) != null)

{

Element elem_m = (Element) id.getElementsByTagName

("M").item(0);

String m = elem_m.getFirstChild().getNodeValue();

}

catch (SAXException sxe){sxe.printStackTrace();}

Former Member
0 Kudos

Hello Lakshminarayana,

Try this (untested) example. If it works and you like it, improve it.

Regards.

....

String number;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);

DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(strXML);

NodeList items = document.getFirstChild().getChildNodes();

for (int x = 0; x < items.getLength(); x++) {

		Node nodo = items.item(x);

		if (nodo.getNodeName() == "RES") number = getNumber(nodo);
		

}

....

private String getNumber(Node nodo){

	String number;
	
	NodeList items = nodo.getChildNodes();

	for (int x = 0; x < items.getLength(); x++) {

		Node child = items.item(x);
			
		if (child.getNodeName() == "M") number = child.getFirstChild().getNodeValue();
		
		//This will work only if you have just 1 M node in a RES node.
		
	}
	
	return number;

}