on 10-18-2005 9:39 AM
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
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();}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
77 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.