使用SAX解析XML空标记标记、SAX、XML

2023-09-07 04:32:13 作者:看她长的那叫一个伤胃

如何找到或SAX解析分开空的XML字符串。

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>

看上面的XML data.In表2,城市是空的标记。由于这个我此行中获得下一行城市名称。例如,我得到第三个表中的数据到第二个表。即野牛正在显示在空数据的地方。我的问题是如何分离或发现SAX解析空标记的数据?我的code是here.

推荐答案

我已经适应从previous回答我的code采取考虑空的XML标记:

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.