0

how to find or separate the empty xml strings in SAX Parsing.

    <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>

Look at the above Xml data.In Table2, City is empty tag. due to this i am getting next row city name in this row. For Example, I am getting 3rd table data into second table. i.e "Bison" is displaying in the place of empty data. My question is how to separate or find the empty tag data in sax parsing? my code is here.

4

2 回答 2

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 是空白的。所有工作..

于 2012-11-28T11:09:23.957 回答
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;
        }

    }

}

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

于 2012-11-28T14:44:07.547 回答