tags:

views:

44

answers:

0

Hi friends

I am getting the value from webservice .i am getting string .i want to parse value

my values are like

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
<soap:Body>
<LoginResponse xmlns="http://tempuri.org/QuestIPhoneWebService/QuestIPhoneWebService"&gt;
<LoginResult>
&lt;ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"&gt;&lt;LOGIN_DETAILS USER_ID="hcltestpub2" COMPANY_ID="1" USER_NAME="HCL aaa" SYSTEM_USER_ID="6976" USER_EMAIL_ID="[email protected]" TOKEN_STRING="A93805F1F1C340F5A8155FDD9B77E595" DISCLAIMER_AGREED="1" USER_ENABLED="1" USER_COMPANY_ENABLED="1" USER_TYPE="2" LOGIN_EXPIRY_DAYS="999" TOKEN_CREATION_DATE="2010-10-01T16:04:26" MOBILE_ENABLED="1" USER_COMPANY_MOBILE_ENABLED="1"/&gt;&lt;COMPANY_DETAILS CLIENT_TYPE_ID="8"/&gt;&lt;USER_SETTINGS&gt;&lt;QUEST_GROUP ID="14293" NAME="World" ASSIGN_NUM="14"/&gt;&lt;INDEX_PROVIDER ID="14251" NAME="QUEST (Default)"/&gt;&lt;STOCK_IDENTIFIER ID="57" NAME="TICKER"/&gt;&lt;/USER_SETTINGS&gt;&lt;PERMISSIONS&gt;&lt;QUEST_FUNCTIONS&gt;&lt;FUNCTION NAME="charting" ID="501" ACCESS="1"/&gt;&lt;FUNCTION NAME="modeller" ID="512" ACCESS="1"/&gt;&lt;FUNCTION NAME="momentum" ID="513" ACCESS="1"/&gt;&lt;FUNCTION NAME="portfolio" ID="516" ACCESS="1"/&gt;&lt;FUNCTION NAME="search" ID="518" ACCESS="1"/&gt;&lt;FUNCTION NAME="sensitivity" ID="521" ACCESS="1"/&gt;&lt;FUNCTION NAME="statistics" ID="524" ACCESS="1"/&gt;&lt;FUNCTION NAME="strategy" ID="525" ACCESS="1"/&gt;&lt;FUNCTION NAME="summary" ID="526" ACCESS="1"/&gt;&lt;FUNCTION NAME="triangle" ID="528" ACCESS="1"/&gt;&lt;FUNCTION NAME="valuation" ID="529" ACCESS="1"/&gt;&lt;FUNCTION NAME="commentary" ID="530" ACCESS="1"/&gt;&lt;FUNCTION NAME="CITN" ID="534" ACCESS="1"/&gt;&lt;FUNCTION NAME="batch report" ID="553" ACCESS="1"/&gt;&lt;FUNCTION NAME="ModellerWS" ID="557" ACCESS="1"/&gt;&lt;FUNCTION NAME="Sector Analysis" ID="562" ACCESS="1"/&gt;&lt;/QUEST_FUNCTIONS&gt;&lt;ADMIN_FUNCTIONS&gt;&lt;FUNCTION NAME="administrator" ID="531" ACCESS="0"/&gt;&lt;FUNCTION NAME="author" ID="532" ACCESS="1"/&gt;&lt;FUNCTION NAME="publisher" ID="533" ACCESS="0"/&gt;&lt;FUNCTION NAME="editor" ID="539" ACCESS="0"/&gt;&lt;/ADMIN_FUNCTIONS&gt;&lt;/PERMISSIONS&gt;&lt;/ROOT&gt;
10-04 14:30:08.696: DEBUG/login result is(439): </LoginResult></LoginResponse></soap:Body></soap:Envelope>

for parsing value i convert to document

package com.inquest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.w3c.dom.Node;

import android.util.Log;

public class DomParser {

    ArrayList listLogin;
    Document doc;
    ArrayList listReturn=new ArrayList();
    public DomParser(){
        //create a list to hold the employee objects
        listLogin = new ArrayList();

    }   


    public ArrayList parseXmlFile(String webservicevalue){
        //get the factory



        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file
            //dom = db.parse(webservicevalue);

            //doc=db.parse(new InputSource(new StringReader(webservicevalue)));
            //dom = db.parse(new InputStream(R.raw.sample);
           // doc=db.parse(line);
              //Document doc = db.parse(file);

              Document doc = 
                    db.parse(new ByteArrayInputStream(webservicevalue.getBytes()));



            //doc.getDocumentElement().normalize();  





            NodeList nodeLst = doc.getElementsByTagName("ROOT");
              //System.out.println("Information of all employees");

              for (int s = 0; s < nodeLst.getLength(); s++) {

                Node fstNode = nodeLst.item(s);

                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

                       Element fstElmnt = (Element) fstNode;
                  NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("LOGIN_DETAILS");
                  Element fstNmElmnt = (Element) fstNmElmntLst.item(s);

                  String userid=fstNmElmnt.getAttribute("USER_ID");
                  String username=fstNmElmnt.getAttribute("USER_NAME");
                  System.out.println("user id1 is"+userid);
                  Log.d("user id is",userid);
                  Log.d("user name is",username);

                 // NodeList fstNm = fstNmElmnt.getChildNodes();
                  //System.out.println("First Name : "  + ((Node) fstNm.item(0)).getNodeValue());

                }

              }









            // NodeList nodeList = dom.getElementsByTagName("LOGIN_DETAILS"); 




        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return listReturn;
    }



private ArrayList getEmployee(Element element) {

    String userid = element.getAttribute("USER_ID");
    String username=element.getAttribute("USER_NAME");      
    String tokenstring=element.getAttribute("TOKEN_STRING");



        DomParserDataSet dataSet=new DomParserDataSet();

         dataSet.setUser_id(userid);
          dataSet.setUser_name(username);
          dataSet.setToken_string(tokenstring);
        listLogin.add(dataSet);

        return listLogin;
    }





}

but i am not able to parse .if i convert to xml file and parse the file in java i can able to parse can anybody tell what is problem any help would be appreciated.i am getting null value?

can anybody tell how to parse xml string ? or how to parse convert xml string to xml file and parse the value give example

any help would be appreciated

Thanks