简体   繁体   English

SAX解析器无法解析我的xml中的照片标签?

[英]SAX Parser not parsing photograph tag in my xml?

I have been using SAX parser to parse an xml file which has been working fine so far but for some reason won't parse the image url in the photograph tag (I want to parse the url as a string) . 我一直在使用SAX解析器来解析到目前为止一直运行良好的xml文件,但是由于某种原因将无法解析图片标签中的图片网址(我想将网址解析为字符串)。 Was hoping someone would know the reason? 希望有人会知道原因吗? any help would be appreciated. 任何帮助,将不胜感激。 This is the XML: 这是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>

and this is the code for my SAX parser: 这是我的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;
    }
}

and more: 和更多:

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;
}

} }

Your characters() method appears to be expecting the whole of a text node to be delivered in a single call. 您的character()方法似乎期望在单个调用中交付整个文本节点。 There's no guarantee that this will happen, and it looks to me as if your particular SAX parser is splitting the text on newline boundaries, which it is perfectly entitled to do. 无法保证会发生这种情况,在我看来,您的特定SAX解析器似乎在换行符边界上拆分文本,这是完全有资格的。 This would mean that the content you get is the content after the last newline. 这意味着您获得的内容是最后一个换行符之后的内容。 You need to buffer up the data passed in successive calls on characters(), and process it in the call on endElement(). 您需要缓冲在characters()上的后续调用中传递的数据,并在endElement()的调用中对其进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM