SAXParser を使用して xml ファイルを解析しています。ただし、私のアプリケーションは、xml ファイルの最初の 3 つの項目のみを解析し、残りは無視します。以下は、解析しようとしているコードと xml ファイルです。
主な活動:
public class XMLParsingExample extends Activity {
/** Create Object For Bar Class */
Bar barList = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView colour[];
TextView division[];
TextView rect[];
try {
/** Handling XML */
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/saveObject/new.xml";
File file = new File(path);
SAXParserFactory parserFactory=SAXParserFactory.newInstance();
SAXParser saxParser=parserFactory.newSAXParser();
XMLReader reader=saxParser.getXMLReader();
MyXMLHandler myXMLHandler = new MyXMLHandler();
reader.setContentHandler(myXMLHandler);
reader.parse(new InputSource(new FileInputStream(file)));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler Bar Object */
barList = MyXMLHandler.barList;
/** Assign textview array lenght by arraylist size */
colour = new TextView[barList.getColour().size()];
division = new TextView[barList.getDivision().size()];
rect = new TextView[barList.getRect().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < barList.getRect().size(); i++) {
colour[i] = new TextView(this);
colour[i].setText("Colour = "+barList.getColour().get(i));
division[i] = new TextView(this);
division[i].setText("Division = "+barList.getDivision().get(i));
rect[i] = new TextView(this);
rect[i].setText("Rect = "+barList.getRect().get(i));
layout.addView(colour[i]);
layout.addView(division[i]);
layout.addView(rect[i]);
}
/** Set the layout view to display */
setContentView(layout);
}
}
XML ハンドラ:
public class MyXMLHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue = null;
public static Bar barList = null;
public static Bar getbarList() {
return barList;
}
public static void setbarList(Bar barList) {
MyXMLHandler.barList = barList;
}
/** Called when tag starts*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("bar"))
{
/** Start */
barList = new Bar();
}
}
/** Called when tag closing*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
/** set value */
if (localName.equalsIgnoreCase("colour"))
barList.setColour(currentValue);
else if (localName.equalsIgnoreCase("division"))
barList.setDivision(currentValue);
else if (localName.equalsIgnoreCase("rect"))
barList.setRect(currentValue);
}
/** Called to get tag characters */
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
バークラス
public class Bar {
/** Variables */
private ArrayList<String> colour = new ArrayList<String>();
private ArrayList<String> division = new ArrayList<String>();
private ArrayList<String> rect = new ArrayList<String>();
/** In Setter method default it will return arraylist
* change that to add */
public ArrayList<String> getColour() {
return colour;
}
public void setColour(String name) {
this.colour.add(name);
}
public ArrayList<String> getDivision() {
return division;
}
public void setDivision(String division) {
this.division.add(division);
}
public ArrayList<String> getRect() {
return rect;
}
public void setRect(String rect) {
this.rect.add(rect);
}
}
解析する XML ファイル:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<bar>
<colour>Blue</colour>
<division>8898</division>
<rect>33</rect>
</bar>
<bar>
<colour>Red</colour>
<division>65</division>
<rect>6876</rect>
</bar>
<bar>
<colour>Black</colour>
<division>65</division>
<rect>6876</rect>
</bar>
<bar>
<colour>Yellow</colour>
<division>65</division>
<rect>6876</rect>
</bar>
<bar>
<colour>Red</colour>
<division>65</division>
<rect>6876</rect>
</bar>
誰が私が間違っているのかを見ることができますか?