XML Empty tags using Sax parsing

不羁的心 提交于 2019-12-10 12:08:10

问题


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.


回答1:


I've adapted my code from the previous answer to take in consideration empty xml tags:

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

    }

}

See if it works. You should try to wrap your many lists into a single list of a simple data holder class.




回答2:


Hi please try this it is working fine.

Your XML file i have put it to in RAW Folder and Name "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>

Create Public Class Beans as below

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 Class as below.

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);
    }

}

Main Activity Below.

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();
        }

    }

}

Now Check data1 I found City is blank. all working..



来源:https://stackoverflow.com/questions/13602752/xml-empty-tags-using-sax-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!