- String res = (_responseMessage.toString());//
true ................ - String responce = res.trim();
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- XMLDOMUtil xml = new XMLDOMUtil();
- ByteArrayInputStream bis = new ByteArrayInputStream(responce.getBytes("UTF-8"));
- Document document = builder.parse(bis);
- Node root = xml.getNodeByTag(document, "root");
- String status = xml.getNodeTextByTag(root ,"status");
- //XMLDOMUtil class is given below.....
- import org.w3c.dom.Node;
- import org.w3c.dom.Text;
- public class XMLDOMUtil {
- // go thru the list of childs and find the text associated by the tag
- public String getNodeTextByTag(Node parentNode, String name) {
- Node node = parentNode.getFirstChild();
- Text text = null;
- String retStr = null;
- try {
- while (node != null) {
- if (node.getNodeName().equals(name)) {
- text = (Text) node.getFirstChild();
- retStr = text.getData();
- break;
- }
- node = node.getNextSibling();
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return retStr;
- }
- public Node getNodeByTag(Node parentNode, String name) {
- Node node = parentNode.getFirstChild();
- Node retNode = null;
- while (node != null) {
- if (node.getNodeName().equals(name)) {
- retNode = node;
- break;
- }
- node = node.getNextSibling();
- }
- return retNode;
- }
- public Node getNextSiblingNodeByTag(Node siblingNode, String name) {
- Node retNode = null;
- siblingNode = siblingNode.getNextSibling();
- while (siblingNode != null) {
- if (siblingNode.getNodeName().equals(name)) {
- retNode = siblingNode;
- break;
- }
- siblingNode = siblingNode.getNextSibling();
- }
- return retNode;
- }
- }
Monday, April 16, 2012
XML parsing in Blackberry
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment