0
我正在使用SAXParser來解析xml文件。但是我的應用程序只有在解析我的XML文件中的第3項,而忽略rest.Below是我的代碼,我試圖解析XML文件:SAXParser不會解析整個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>
任何人都可以看到我要去哪裏錯了嗎?
做喲實際上發佈整個XML? – Blackbelt 2013-02-25 16:37:45
有什麼特別的原因,你爲什麼要存儲顏色,divions和rects的集合,而不是存儲一組酒吧? – Perception 2013-02-25 16:37:57
是的,這是整個XML。我正在嘗試存儲具有上述屬性的酒吧集合。我做錯了嗎? – DMC 2013-02-25 16:43:05