使用Sax解析的XML Empty标签

时间:2012-11-28 10:19:48

标签: android xml listview xml-parsing sax

如何在SAX Parsing中找到或分离空的xml字符串。

    <Table diffgr:id="Table142" msdata:rowOrder="141">
    <TeamIDGiven>7</TeamIDGiven>
    <City>Wolfpack</City>
    <TeamName>North Carolina State</TeamName>
    </Table>

    <Table diffgr:id="Table143" msdata:rowOrder="142">
    <TeamIDGiven>308</TeamIDGiven>
    **<City/>**
    <TeamName>North Dakota</TeamName>

    </Table> 

   <Table diffgr:id="Table144" msdata:rowOrder="143">
   <TeamIDGiven>309</TeamIDGiven>
   <City>Bison</City>
   <TeamName>North Dakota State</TeamName>
  </Table>

查看上面的Xml数据。在表2中,City是空标记。因为这个我在这一行得到下一行城市名称。 例如,我将第三个表数据输入第二个表。即“Bison”显示在空数据的位置。 我的问题是如何在sax解析中分离或找到空标签数据? 我的代码是here

2 个答案:

答案 0 :(得分:4)

嗨,请试试这个工作正常。

您的XML文件我已将其放入 RAW文件夹和名称“abc.xml”

<?xml version="1.0" encoding="utf-8"?>
<Tables><Table id="Table142" rowOrder="141">
    <TeamIDGiven>7</TeamIDGiven>
    <City>Wolfpack</City>
    <TeamName>North Carolina State</TeamName>
    </Table>

    <Table id="Table143" rowOrder="142">
    <TeamIDGiven>308</TeamIDGiven>
    <City/>
    <TeamName>North Dakota</TeamName>

    </Table> 

   <Table id="Table144" rowOrder="143">
   <TeamIDGiven>309</TeamIDGiven>
   <City>Bison</City>
   <TeamName>North Dakota State</TeamName>
  </Table></Tables>

创建公共类Bean,如下所示

public class Table_Beans {
    private String TeamIDGiven;
    private String City;
    private String TeamName;

    public String getTeamIDGiven() {
        return TeamIDGiven;
    }

    public void setTeamIDGiven(String teamIDGiven) {
        TeamIDGiven = teamIDGiven;
    }

    public String getCity() {
        return City;
    }

    public void setCity(String city) {
        City = city;
    }

    public String getTeamName() {
        return TeamName;
    }

    public void setTeamName(String teamName) {
        TeamName = teamName;
    }

}

XMLhandler类如下。

public class TestXMLhandler extends DefaultHandler {

    private StringBuffer buffer = new StringBuffer();
    private ArrayList<Table_Beans> data_list;
    private Table_Beans data;

    public ArrayList<Table_Beans> getData() {
        return data_list;
    }

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

        buffer.setLength(0);
        if (localName.equals("Tables")) 
            data_list = new ArrayList<Table_Beans>();
        else if (localName.equals("Table")) 
            data = new Table_Beans();
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (localName.equalsIgnoreCase("TeamIDGiven"))
            data.setTeamIDGiven(buffer.toString().trim());
        else if (localName.equalsIgnoreCase("City"))
            data.setCity(buffer.toString().trim());
        else if (localName.equalsIgnoreCase("TeamName"))
            data.setTeamName(buffer.toString().trim());
        else if (localName.equalsIgnoreCase("Table"))
            data_list.add(data);
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        buffer.append(ch, start, length);
    }

}

以下主要活动。

public class Test1 extends Activity {

    private ArrayList<Table_Beans> data1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getData();
    }

    private void getData() {

        try {
            InputSource inSource = new InputSource(getResources()
                    .openRawResource(R.raw.abc));

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

            TestXMLhandler xmlhandler1 = new TestXMLhandler();
            xr.setContentHandler(xmlhandler1);
            xr.parse(inSource);

            data1 = xmlhandler1.getData();

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

    }

}

现在检查 data1 我发现City为空白。一切正常..

答案 1 :(得分:1)

我已经调整了上一个答案的代码,以考虑空的xml标记:

class MyHandler extends DefaultHandler {
    boolean is_sno = false;
    boolean is_sname = false;
    boolean mIsSegment = false;

    int mCurrentIndex = -1;

    @Override
    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        super.startElement(uri, localName, name, attributes);
        if (localName.equals("Table")) {
            mCurrentIndex++;
            al_sno.add("");
            al_sname.add("");
        } else if (localName.equals("TeamIDGiven")) {
            is_sno = true;
        } else if (localName.equals("City")) {
            is_sname = true;
        }

    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {           
        super.characters(ch, start, length);
        if (is_sno) {
            al_sno.set(mCurrentIndex, new String(ch, start, length));
        } else if (is_sname) {
            if (!mIsSegment) {
                al_sname.set(mCurrentIndex, new String(ch, start, length));
            } else {
                al_sname.set(mCurrentIndex,
                        al_sname.get(al_sname.size() - 1)
                                + new String(ch, start, length));
            }
            mIsSegment = true;
        }

    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        // TODO Auto-generated method stub
        super.endElement(uri, localName, name);
        if (localName.equals("TeamIDGiven")) {
            is_sno = false;
        } else if (localName.equals("City")) {
            is_sname = false;
            mIsSegment = false;
        }

    }

}

看看它是否有效。您应该尝试将多个列表包装到一个简单数据持有者类的单个列表中。