1

私はこれまで正常に機能していたxmlファイルを解析するためにSAXパーサーを使用していますが、何らかの理由で写真タグの画像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;
}

}

4

1 に答える 1

0

あなたのcharacters()メソッドは、テキストノード全体が1回の呼び出しで配信されることを期待しているようです。これが発生する保証はありません。特定のSAXパーサーが改行境界でテキストを分割しているように見えますが、これは完全に実行する権利があります。これは、取得するコンテンツが最後の改行の後のコンテンツであることを意味します。character()の連続した呼び出しで渡されたデータをバッファリングし、endElement()の呼び出しで処理する必要があります。

于 2012-12-06T08:39:48.377 に答える