Sax パーサーで複数の文字列タグの連結に問題があります。私のXmlデータは(巨大なリスト)のように見えます..
<Table diffgr:id="Table1" msdata:rowOrder="0">
<ID>213</ID>
<LastName>Sirk</LastName>
<FirstName>Thomas</FirstName>
<Height>6 ft 4 inches</Height>
<HomeTown>Glen St. Mary's</HomeTown>
<TeamName>Blue Devils</TeamName>
<Age>19</Age>
<FullName>1 Thomas Sirk</FullName>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<ID>17</ID>
<LastName>Vernon</LastName>
<FirstName>Conner</FirstName>
<Height>6 ft 1 inches</Height>
<HomeTown>Miami Fl</HomeTown>
<TeamName>Blue Devils</TeamName>
<Age>22</Age>
<FullName>2 Conner Vernon</FullName>
</Table>
<Table diffgr:id="Table3" msdata:rowOrder="2">
<ID>203</ID>
<LastName>Crowder</LastName>
<FirstName>Jamison</FirstName>
<Height>5 ft 9 inches</Height>
<HomeTown>Monroe NC</HomeTown>
<TeamName>Blue Devils</TeamName>
<Age>19</Age>
<FullName>3 Jamison Crowder</FullName>
</Table>
このコードをこのようなハンドラで使用しました。
class MyHandler extends DefaultHandler{
boolean is_sno=false;
boolean is_sname=false;
boolean is_sclass=false;
boolean is_sphno=false;
boolean is_semail=false;
boolean mIsSegment = false;
int mCurrentIndex = -1;
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@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("");
al_sclass.add("");
al_sphno.add("");
al_semail.add("");
}
else if(localName.equals("ID")){
is_sno=true;
}
else if(localName.equals("TeamName")){
is_sname=true;
}
else if(localName.equals("Fullname")){
is_sclass=true;
}
/*else if(localName.equals("HomeTown")){
is_sphno=true;
}
else if(localName.equals("Height")){
is_semail=true;
}*/
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
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;
}
else if(is_sclass){
if (!mIsSegment) {
al_sclass.set(mCurrentIndex, new String(ch, start, length));
} else {
al_sclass.set(mCurrentIndex,
al_sclass.get(al_sclass.size() - 1)
+ new String(ch, start, length));
}
mIsSegment = true;
}
else if(is_sphno){
if (!mIsSegment) {
al_sphno.set(mCurrentIndex, new String(ch, start, length));
} else {
al_sphno.set(mCurrentIndex,
al_sphno.get(al_sphno.size() - 1)
+ new String(ch, start, length));
}
mIsSegment = true;}
}
else if(is_semail){
if (!mIsSegment) {
al_semail.set(mCurrentIndex, new String(ch, start, length));
} else {
al_semail.set(mCurrentIndex,
al_semail.get(al_semail.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("ID")){
is_sno=false;
}
else if(localName.equals("TeamName")){
is_sname=false;
mIsSegment = false;
}
else if(localName.equals("Fullname")){
is_sclass=false;
mIsSegment = false;
}
else if(localName.equals("HomeTown")){
is_sphno=false;
mIsSegment = false;
}
else if(localName.equals("Height")){
is_semail=false;
mIsSegment = false;
}
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
}
チームのプレーヤーリストを 1 ページに表示しようとしていますが、ほとんどの文字列が Height、HomeTown、Fullname、TeamName の連結の問題を抱えています。ここでいくつかのコードに従いました。複数の文字列ではなく、単一の文字列で正常に機能します。私を助けてください。