我正在使用SAXParser来解析xml文件。但是我的应用程序只解析我的xml文件中的前3项并忽略其余的.Below是我的代码和我试图解析的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;
}
}
}
Bar Class
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>
谁能看到我哪里出错了?
答案 0 :(得分:2)
每次创建新的bar实例时,都会丢失旧的bar实例。 如果您尝试存储Bar实例的集合,请更改Bar类,如下所示:
public class Bar {
private String colour;
private String division;
private String rect;
public void setColor(String color) {
this.color = color;
}
//other setters & getters...
}
在MyXmlHandler中,有一个List和一个当前Bar:
public List<Bar> barList = new ArrayList<Bar>();
private Bar currentBar;
在startElement事件处理程序中,执行以下操作:
if (localName.equals("bar"))
{
/** Start */
currentBar = new Bar();
barList.add(currentBar);
}
在endElement事件处理程序中,
if (localName.equalsIgnoreCase("colour"))
currentBar.setColour(currentValue);
else if (localName.equalsIgnoreCase("division"))
currentBar.setDivision(currentValue);
else if (localName.equalsIgnoreCase("rect"))
currentBar.setRect(currentValue);
应该可以做到这一点,虽然我没有测试过它