这个xml解析器(Android)出了什么问题?

时间:2012-12-05 22:35:45

标签: android xml parsing sax

我想解析一个xml文件。我试图找到错误,但我找不到它。你能帮我吗,请问? XML:

<maintag>
    <data>
        <id>1</id>
        <name>x y</name>
        <age>16</age>
        <phone>06/30 123-4567</phone>
        <address>Veszprem Valami ut 10.</address>
    </data>
    <data>
        <id>2</id>
        <name>p q</name>
        <age>18</age>
        <phone>06/70 987-6543</phone>
        <address>Budapest Ulloi ut 21.</address>
    </data>
</maintag>

XMLParser类:

package com.example.xmlproba;

import java.util.jar.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;

public class XMLParser extends DefaultHandler {

    boolean xmlStartLine = false;
    boolean id = false;
    boolean data = false;
    boolean maintag = false;
    boolean age = false;
    boolean name = false;
    boolean phone = false;
    boolean address = false;

    Data currentData;
    DataContainer dataContainer;

    public XMLParser(DataContainer dataContainer){
        this.dataContainer=dataContainer;
        Log.d("FUNC","XMLPARSER()");

        // Data d = new Data();
    }

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        Log.d("START","START");

        if (qName.equalsIgnoreCase("MAINTAG")) {
            Log.d("Maintagfound","mtf");

        } else if (qName.equalsIgnoreCase("DATA")) {
            //create new data object 
            currentData = new Data();
            Log.d("NEWDATA","NEWDATA");
        } else if (qName.equalsIgnoreCase("ID")) {
            id  = true;
            Log.d("id","id");
        } else if (qName.equalsIgnoreCase("NAME")) {
            name = true;
            Log.d("name","name");
        } else if (qName.equalsIgnoreCase("AGE")) {
            age = true;
        } else if (qName.equalsIgnoreCase("PHONE")) {
            phone = true;
        } else if (qName.equalsIgnoreCase("ADDRESS")) {
            address = true;
        } else {
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        Log.d("END","END");
        if (qName.equalsIgnoreCase("DATA")) {
            //todo at the end of a data node
            //dataContainer.addDataToList(currentData);
            //dataContainer.dataList.add(currentData);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        Log.d("CHARS","CHARS");
        if (id) {
            String s = new String(ch, start, length);
            currentData.setId(Integer.parseInt(s));
            Log.d("IDIDID",s);
            id = false;
        } else if (name) {
            String s = new String(ch, start, length);
            Log.d("NAME1",s);
            currentData.setName(s);
            Log.d("NAME2",currentData.getName());
            name = false;
        } else if (age) {
            String s = new String(ch, start, length);
            currentData.setAge(Integer.parseInt(s));

            age = false;
        } else if (phone) {
            String s = new String(ch, start, length);
            currentData.setPhone(s);
            phone = false;
        } else if (address) {
            String s = new String(ch, start, length);
            currentData.setAdress(s);
            address = false;
        } else {
        }

    }

}

我的活动的解析部分:

private void readXML() {
    // TODO Auto-generated method stub
    Log.d("FUNC","READXML");
    try {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        XMLParser xp = new XMLParser(dataContainer);
        xr.setContentHandler(xp);
        InputStream is = getResources().openRawResource(
                R.raw.data);
        xr.parse(new InputSource(is));

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        Log.d("PARSERCONFEX","PARSERCONFEX");
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        Log.d("SAXEX","SAXEX");
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.d("IOEX","IOEX");
        e.printStackTrace();
    }

}

从log.d()我可以看到只创建了构造函数的日志,“CHARS”和“END”日志。 这是我的日志:

12-06 06:38:11.063: E/Trace(629): error opening trace file: No such file or directory (2)
12-06 06:38:11.623: D/FUNC(629): INIT
12-06 06:38:11.623: D/FUNC(629): READXML
12-06 06:38:11.643: D/FUNC(629): XMLPARSER()
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.673: D/END(629): END
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/END(629): END
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/END(629): END
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/END(629): END
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/END(629): END
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/END(629): END
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.703: D/CHARS(629): CHARS
12-06 06:38:11.703: D/END(629): END
12-06 06:38:11.703: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/END(629): END
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/END(629): END
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/END(629): END
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/END(629): END
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/END(629): END
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/END(629): END
12-06 06:38:12.063: I/Choreographer(629): Skipped 77 frames!  The application may be doing too much work on its main thread.
12-06 06:38:12.093: D/gralloc_goldfish(629): Emulator without GPU emulation detected.

1 个答案:

答案 0 :(得分:0)

这是一个使用SAX想要做的事情的例子

JAVA CLASSES

数据

public class Data {

    private Integer id;
    private String name;
    private Integer age;
    private String phone;
    private String address;


    public Data(){}


    public Integer getId() {
        return id;
    }


    public void setId(Integer id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getPhone() {
        return phone;
    }


    public void setPhone(String phone) {
        this.phone = phone;
    }


    public String getAddress() {
        return address;
    }


    public void setAddress(String address) {
        this.address = address;
    }


    public Integer getAge() {
        return age;
    }


    public void setAge(Integer age) {
        this.age = age;
    }


}

<强> DatasHandler

public class DatasHandler extends DefaultHandler {

    private ArrayList<Data> alDatas;
    private Data data;
    private String reading;

    public DatasHandler(){}

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        if(qName.equals("maintag")){
            alDatas = new ArrayList<>();
        }
        if(qName.equals("data")){
            data = new Data();
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        if(qName.equals("data")){
            alDatas.add(data);
            data = null;
        }
        if(qName.equals("id")){
            data.setId(Integer.parseInt(reading));
        }
        if(qName.equals("name")){
            data.setName(reading);
        }
        if(qName.equals("age")){
            data.setAge(Integer.parseInt(reading));
        }
        if(qName.equals("phone")){
            data.setPhone(reading);
        }
        if(qName.equals("address")){
            data.setAddress(reading);
        }

    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        reading = new String(ch, start, length);
    }

    public ArrayList<Data> getAlDatas() {
        return alDatas;
    }


}

<强> XMLManager

public final class XMLManager {

    public static ArrayList<Data> getAlDatas(){
        ArrayList<Data> alDatas = null;
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            SAXParser parser = factory.newSAXParser();
            DatasHandler dHandler = new DatasHandler();
            parser.parse(new File("D:\\Loic_Workspace\\SaxExample\\res\\test.xml"), dHandler);
            alDatas = dHandler.getAlDatas();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return alDatas;
    }


}

主要

public class MyMain {

    /**
     * @param args
     */
    public static void main(String[] args) {

        ArrayList<Data> alDatas = XMLManager.getAlDatas();

        for(Data d:alDatas){
            System.out.println(d.getName());
            System.out.println(d.getId());
            System.out.println(d.getAge());
            System.out.println(d.getPhone());
            System.out.println(d.getAddress());
            System.out.println("--------------------");
        }

    }

}

控制台输出

x y
1
16
06/30 123-4567
Veszprem Valami ut 10.
--------------------
p q
2
18
06/70 987-6543
Budapest Ulloi ut 21.
--------------------

您似乎想在Android上使用它。所以我认为您可以在XMLManager类中执行以下操作:

InputSource in = new InputSource(getResources().openRawResource(R.raw.data));
parser.parse(in, dHandler);

告诉我它是否有效,