简体   繁体   中英

Unable to get Value when i do XMl Parsing in android

This is XML Parser Class:

public class XMLParser {

    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML from URL making HTTP request
     * 
     * @param url
     *            string
     * */
    public String getXmlFromUrl(String url)

    {
        String xml = null;

        try

        {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();

            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpGet, localContext);

            HttpEntity entity = response.getEntity();

            xml = getASCIIContentFromEntity(entity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

    // Parsing the XML content and retrieve DOM element in the XML.

    private String getASCIIContentFromEntity(HttpEntity entity)
            throws IllegalStateException, IOException {
        InputStream in = entity.getContent();

        StringBuffer out = new StringBuffer();
        int n = 1;
        while (n > 0) {
            byte[] b = new byte[4096];
            n = in.read(b);

            if (n > 0)
                out.append(new String(b, 0, n));
        }

        return out.toString();
    }

    public Document getDomElement(String xml)

    {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try

        {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error", e.getMessage());
            return null;
        }

        return doc;
    }

    /**
     * Getting node value
     * 
     * @param elem
     *            element
     */
    public final String getElementValue(Node elem) {
        Node child;
        if (elem != null) {
            if (elem.hasChildNodes()) {
                for (child = elem.getFirstChild(); child != null; child = child
                        .getNextSibling()) {
                    if (child.getNodeType() == Node.TEXT_NODE) {
                        return child.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

    // Retrieve each element child element value by using node name of element.
    public String getValue(Element item, String str) {      
        NodeList n = item.getElementsByTagName(str);        
        return this.getElementValue(n.item(0));
    }
}

This class where i want to get value :

String resultFromGetXmlFromUrl = parser.getXmlFromUrl(URL);
        String xml = Html.fromHtml(resultFromGetXmlFromUrl).toString();
        Document doc = parser.getDomElement(xml); // getting DOM element
        String value = null;
        NodeList list=doc.getElementsByTagName("Table");
         for (int temp = 0; temp < list.getLength(); temp++) 
         {

              Element e = (Element) list.item(temp);
              String Value=parser.getValue(e, "code");
         }

But here am getting Error : String Value=parser.getValue(e, "code");

Compile time error in getValue i don't know where am doing mistake this is my XML Content:

<NewDataSet>
  <Table>
    <code>91</code>
    <name>India</name>
  </Table>
  <Table>
    <code>91</code>
    <name>India</name>
  </Table>
</NewDataSet>

i am trying to get value code: 91

getValue()的第一个参数需要org.w3c.dom.Element类型的元素,但是您的第一个参数类型是android.renderscript.Element ,将类型转换更改为:

org.w3c.dom.Element e = (org.w3c.dom.Element) list.item(temp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM