I am trying to parse an XML file, but I am having some trouble fixing the errors I am receiving

I have been trying to figure out how to figure out how I can solve the errors that I am receiving while trying to parse an XML file.

This is my MainActivity.java file:

package com.example.android;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.renderscript.Element;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    String playerId,
    DefaultHttpClient httpClient;
    HttpPost httpPost;
    HttpResponse httpResponse;
    HttpEntity httpEntity;
    Document doc;
    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    InputSource inputSource;
    NodeList n,
    Node child;
    XmlPullParser parser;
    static final String URL = ".xml URL WITHELD";
    static final String PLAYERS = "Players"; //parent node
    static final String PLAYER_PD = "PlayerPD";
    static final String PLAYER_TYPE = "PlayerType";     

    protected void onCreate(Bundle savedInstanceState) {
        //Remove title bar

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;

    public String getXmlFromUrl(String url){
        xml = null;
            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost (url);

            httpResponse = httpClient.execute(httpPost);
            httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);         
        } catch (UnsupportedEncodingException e) {
        } catch (ClientProtocolException e) {
        } catch (IOException e){
        //return XML
        return xml; 

    public Document getDomElement(String xml){
        doc = null;
        dbf = DocumentBuilderFactory.newInstance();
            db = dbf.newDocumentBuilder();

            inputSource = new InputSource();
            inputSource.setCharacterStream(new StringReader (xml)); 
            doc = db.parse(inputSource);
        } 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 DOM
        return doc;     

    public String getValue(Element item, String str){
        n = ((Document) item).getElementsByTagName(str);
        return this.getElementValue(n.item(0));     

    private final String getElementValue(Node elem){
        // TODO Auto-generated method stub
            for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){
                if(child.getNodeType() == Node.TEXT_NODE){
                    return child.getNodeValue();
        return null;        

   public void parseXml(){

      xml = parser.getXmlFromUrl(URL); //Getting XML

On this line, Line 149, I receive an error that says"The method getXmlFromUrl(String) is undefined for the type XmlPullParser"

      doc = parser.getDomElement(xml); //Getting DOM element

And, on this line, Line 150, I receive an error that says, "The method getDomElement(String) is undefined for the type XmlPullParser"

      nl = doc.getElementsByTagName(PLAYERS);

      for (int i = 0; i < nl.getLength(); i++){
      playerPd = parser.getValue(e, PLAYER_PD); //PlayerPD child value

Also, on this line and the one that follows it, Lines 155 & 156, I receive an error that says, "e cannot be resolved to a variable"

      playerType = parser.getValue(e, PLAYER_TYPE); //Player Type child value


If this helps, the layout of the XML file I am trying to parse is like this:

  -<Player id="">

Any help would be appreciated, and thanks in advance.


Remove parser.

public void parseXml(){

      xml = getXmlFromUrl(URL); //Getting XML

Or you can create a class XMLParser and add getXmlFromUrl function inside the new class

Similarly for other functions

doc = getDomElement(xml); //Getting DOM element

your lines 155 & 156 are not in the full source code so I'm not sure the context.

So you've mentioned the original tutorial. What the tutorial means is you need to create a new file XMLParser.java containing new class called XMLParser

public class XMLParser {

Then slowly you add functions to that class

public class XMLParser {
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

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

So on and so forth, adding the other functions to the class. Then you can use the class from your MainActivity class.

As for e cannot be resolved to a variable, there's a missing line in the web page.

NodeList nl = doc.getElementsByTagName(KEY_ITEM);

// looping through all item nodes <item>     
for (int i = 0; i < nl.getLength(); i++) {
    Element e = (Element)nl.item(i); // >>> the missing line in the sample <<<
    String name = parser.getValue(e, KEY_NAME); // name child value
    String cost = parser.getValue(e, KEY_COST); // cost child value
    String description = parser.getValue(e, KEY_DESC); // description child value

Need Your Help

SQLConnection connection error “Login failed for user server/guest”

c# sql-server authentication login sqlconnection

I keep receiving a connection error. I know that the information is right, as I tried them out with SSMS now several times in the last few minutes, so the problem is in the C# connection string.

Simple flat shading using Stage3D/AGAL

actionscript 3d stage3d agal

I'm relatively new to 3D development and am currently using Actionscript, Stage3D and AGAL to learn. I'm trying to create a scene with a simple procedural mesh that is flat shaded. However, I'm stu...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.