Question

I am trying to simply xml parsing using SAX parser.When i do Run it did not show result.I have given xml file into assets folder.But why it is not work I can not understand.Please help me.

MainActivity.java

package com.example.backgroud_ahsanul;

import java.io.InputStream;
import java.util.jar.Attributes;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt=(TextView)findViewById(R.id.textView1);

        try {
            SAXParserFactory factory=SAXParserFactory.newInstance();
            SAXParser saxParser=factory.newSAXParser();
            DefaultHandler handler=new DefaultHandler(){
            boolean name=false;
            boolean salary=false;

            public void startElement(String uri, String localName,
                    String qName, org.xml.sax.Attributes attributes) throws org.xml.sax.SAXException {
                if(qName.equalsIgnoreCase("name")){
                    name=true;
                }
               if(qName.equalsIgnoreCase("salary")){
                   salary=true;
               }

            }

            public void endElement(String uri, String localName, String qName) throws org.xml.sax.SAXException {



            }

            @Override
                public void characters(char[] ch, int start, int length)
                        throws SAXException {
                    // TODO Auto-generated method stub
                    super.characters(ch, start, length);
                    if(name){
                        txt.setText(txt.getText()+"\n\n Name: "+new String(ch,start,length));
                        Toast.makeText(getApplicationContext(), "name", Toast.LENGTH_LONG).show();
                        name=false;
                    }
                    if(salary){
                        txt.setText(txt.getText()+"\n\n Salary: "+new String(ch,start,length));
                        Toast.makeText(getApplicationContext(), "salary", Toast.LENGTH_LONG).show();
                        salary=false;
                    }

                }


            };

            InputStream is=getAssets().open("file.xml");
            saxParser.parse(is, handler);

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


    }


}

layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="76dp"
        android:layout_marginTop="73dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

IF you want to see my document file you get this link: http://codepad.org/SAnFP1hr

Was it helpful?

Solution

Got it! The XML parser hates white space before the first tag.

Open file.xml and delete the white space before the first tag.

The XML you provided omitted that and that's why it worked for me!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top