簡體   English   中英

SAX解析器無法解析我的xml中的照片標簽?

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

我一直在使用SAX解析器來解析到目前為止一直運行良好的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>

這是我的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;
}

}

您的character()方法似乎期望在單個調用中交付整個文本節點。 無法保證會發生這種情況,在我看來,您的特定SAX解析器似乎在換行符邊界上拆分文本,這是完全有資格的。 這意味着您獲得的內容是最后一個換行符之后的內容。 您需要緩沖在characters()上的后續調用中傳遞的數據,並在endElement()的調用中對其進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM