我一直在使用 SAX 解析器来解析一个 xml 文件,该文件到目前为止工作正常,但由于某种原因不会解析照片标签中的图像 url(我想将 url 解析为字符串)。希望有人知道原因吗?任何帮助,将不胜感激。这是 XML:
<candidate>
<name>Scott Web</name>
<office>Vice President (Academic Affairs)</office>
<photograph>
http://www.inf.ed.ac.uk/teaching/courses/selp/elections/ScottWeb.jpg
</photograph>
<promises>
<promise>University 2.0!</promise>
<promise>Poducation!</promise>
<promise>Lectures as tweets!</promise>
<promise>Personal Tutors on Skype</promise>
<promise>Grades on IM!</promise>
<promise>Feedback HD</promise>
<promise>360-degree learning portal</promise>
</promises>
<statement>I have made it my mission to ensure that this university gets an upgrade and defrag. Lectures and tutorials in the 21st century need to be on-line and interactive. Vote for the future: vote for Scott Web!</statement>
</candidate>
这是我的 SAX 解析器的代码:
public class XMLHandler extends DefaultHandler {
String elementValue = "";
Boolean elementOn = false;
Boolean inPromises = false;
public static XMLGettersSetters data = null;
public static ArrayList<XMLGettersSetters> candList = new ArrayList<XMLGettersSetters>();
public static ArrayList<XMLGettersSetters> getCandList() {
return candList;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
elementOn = true;
if (localName.equals("candidate"))
{
data = new XMLGettersSetters();
} else if (localName.equals("promises")) {
inPromises = true;
}
}
/**
* This will be called when the tags of the XML end.
**/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
elementOn = false;
/**
* Sets the values after retrieving the values from the XML tags
* */
if (localName.equalsIgnoreCase("name"))
data.setName(elementValue);
else if (localName.equalsIgnoreCase("office"))
data.setOffice(elementValue);
else if (localName.equalsIgnoreCase("photograph"))
data.setPhotograph(elementValue);
else if (localName.equalsIgnoreCase("promise")) {
if(inPromises){
data.setPromise(elementValue);
}
}
else if (localName.equalsIgnoreCase("statement"))
data.setStatement(elementValue);
else if(localName.equalsIgnoreCase("candidate"))
candList.add(data);
}
/**
* This is called to get the tags value
**/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (elementOn) {
elementValue = new String(ch, start, length);
elementOn = false;
}
}
和更多:
public class XMLGettersSetters {
private String name = null;
private String office = null;
private String photograph = null;
private ArrayList<String> promise = new ArrayList<String>();
private String statement = null;
private String promises = null;
public XMLGettersSetters() {
}
public XMLGettersSetters(String name, String office, String photograph, String promises, String statement) {
this.name = name;
this.office = office;
this.photograph = photograph;
this.promises = promises;
this.statement = statement;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
Log.i("This is the name:", name);
}
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office=office;
Log.i("This is the office:", office);
}
public String getPhotograph() {
return photograph;
}
public void setPhotograph(String photograph) {
this.photograph=photograph.trim();
Log.i("This is the photo url:", photograph);
}
public ArrayList<String> getPromise() {
return promise;
}
public void setPromise(String promise) {
this.promise.add(promise);
Log.i("This is the promise:", promise);
}
public String getStatement() {
return statement;
}
public void setStatement(String statement) {
this.statement=statement;
Log.i("This is the statement:", statement);
}
public String getPromises() {
return promises;
}
public void setPromises(String promises) {
this.promises = promises;
}
}