2014-01-26 275 views
0

嗨即時通訊困難搞清楚要做我的XML文件讀取。XML解析與SAX

即時通訊使用薩克斯,但我似乎無法得到SAX讀取「ansArray」的子節點。林不知道如果我的XML格式錯誤,任何幫助將是偉大的。

NB 通過使用XStream保存XML文檔,這就是爲什麼「AnsArray」有4個字符串項目的原因。

XML DOC:

<?xml version="1.0" encoding="utf-8" ?> 
    <questionList> 
    <Question> 
     <id>-1</id> 
     <question>&quot;Who Was Sleepless In Seattle With Meg Ryan?&quot;</question> 
     <ansArray> 
     <string>&quot;Bruce Willis&quot;</string> 
     <string>&quot;Arnie Schwarzenegger&quot;</string> 
     <string>&quot;Joe Pesci&quot;</string> 
     <string>&quot;Tom Hanks&quot;</string> 
     </ansArray> 
     <correctAns>4</correctAns> 
     <iDifficulty>2</iDifficulty> 
    </Question> 
    <Question> 
     <id>-1</id> 
     <question>&quot;who was the Terminator?&quot;</question> 
     <ansArray> 
     <string>&quot;John Travolta&quot;</string> 
     <string>&quot;Stevan Segal&quot;</string> 
     <string>&quot;Arnie Schwarzenegger&quot;</string> 
     <string>&quot;Al Pacino&quot;</string> 
     </ansArray> 
     <correctAns>3</correctAns> 
     <iDifficulty>3</iDifficulty> 
    </Question> 
</questionList> 

在上面的XML文件,我可以讀除了ansArray的孩子不一一切即

<ansArray> 
    <string>&quot;John Travolta&quot;</string> 
    <string>&quot;Stevan Segal&quot;</string> 
    <string>&quot;Arnie Schwarzenegger&quot;</string> 
    <string>&quot;Al Pacino&quot;</string> 
    </ansArray> 

SAX XML代碼:

import java.util.ArrayList; 
import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

public class SAX_XMLReader extends DefaultHandler { 

    boolean currentElement = false; 
    String currentValue = ""; 

    static Question question; 
    static ArrayList<Question> questionList=new ArrayList<Question>(); 

    ArrayList<String> sAnswers=new ArrayList<String>(4); 

    public static ArrayList<Question> getquestionList() { 
     return questionList; 
    } 

    @Override 
    public void startElement(String uri, String localName, String qName, 
      Attributes atts) throws SAXException { 

     currentElement = true; 

     //System.out.println("qName = "+qName); 

     if (qName.equals("questionList")) { 
      questionList = new ArrayList<Question>(); 
     } else if (qName.equals("Question")) { 
      question = new Question(); 
     } //how to handle the answer string array 
     else if (qName.equalsIgnoreCase("ansArray")) { 
       //?? 
     } 
    } 

    @Override 
    public void endElement(String uri, String localName, String qName) 
      throws SAXException { 

     currentElement = false; 

     //System.out.println("qName = "+qName); 

     if(qName.equals("question")) 
     { 
      question.setQuestion(currentValue.trim()); 
      //System.out.println("Q: "+currentValue.trim()); 
     } 
     else if (qName.equalsIgnoreCase("correctAns")) { 
      question.setCorrectAns(Integer.parseInt(currentValue.trim()));    
     } 
     else if (qName.equalsIgnoreCase("iDifficulty")) { 
      question.setiDifficulty(Integer.parseInt(currentValue.trim()));   
     }  
     else if (qName.equalsIgnoreCase("ansArray")) { 
      //?? 
     }    

     else if (qName.equals("Question")) { 
      questionList.add(question); 
     } 

     currentValue = "";    

    } 

    @Override 
    public void characters(char[] ch, int start, int length) 
      throws SAXException { 

     if (currentElement) { 
      currentValue = currentValue + new String(ch, start, length); 
     } 

    } 

} 

問題類:

import com.thoughtworks.xstream.annotations.XStreamAlias; 
import com.thoughtworks.xstream.XStream; 
import com.thoughtworks.xstream.annotations.XStreamImplicit; 
import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

//change string array to list 

public class Question implements Serializable{ 

    private int id=-1;   
    private String question="";   
    //private String[] ansArray = new String[4];   
     ArrayList<String> sAnswers=new ArrayList<String>(4);  
    private int correctAns=-1; 
    private int iDifficulty=-1; 


    public Question(){ 
      //sAnswers=new ArrayList<String>(); 
      for(int x=0;x<4;x++){ 
       sAnswers.add("default answer "+x); 
      } 
    } 

    //testing contructor 
     public Question(int id,String sQuestion){ 

       this.id=id; 
     this.question =sQuestion; 
    } 


    public Question(int id,String sQuestion,String sAns1){ 

       this.id=id; 
     this.question =sQuestion; 
       this.sAnswers.add(sAns1); 

     //this.ansArray[0]=sAns1; 
    } 

    public Question(String sQuestion,String sAns1,String sAns2,String sAns3,String sAns4,int iCorrectAns){ 

       this.question =sQuestion; 
     this.sAnswers.add(sAns1); 
       this.sAnswers.add(sAns2); 
       this.sAnswers.add(sAns3); 
       this.sAnswers.add(sAns4); 
     this.correctAns =iCorrectAns; 
    } 

    //constuctor with difficuly int(1-15) 
    public Question(String sQuestion,String sAns1,String sAns2,String sAns3,String sAns4,int iCorrectAns,int iDifficulty){ 

       this.question =sQuestion; 
     this.sAnswers.add(sAns1); 
       this.sAnswers.add(sAns2); 
       this.sAnswers.add(sAns3); 
       this.sAnswers.add(sAns4); 
     this.correctAns =iCorrectAns; 
     this.iDifficulty=iDifficulty; 
    } 

    //constuctor with difficuly int(1-15) 
    public Question(int id,String sQuestion,String sAns1,String sAns2,String sAns3,String sAns4,int iCorrectAns,int iDifficulty){ 

       this.id=id; 
     this.question =sQuestion; 
     this.sAnswers.add(sAns1); 
       this.sAnswers.add(sAns2); 
       this.sAnswers.add(sAns3); 
       this.sAnswers.add(sAns4); 
     this.correctAns =iCorrectAns; 
     this.iDifficulty=iDifficulty; 
    } 



    public int getiDifficulty() { 
     return iDifficulty; 
    } 

    public void setiDifficulty(int iDifficulty) { 
     this.iDifficulty = iDifficulty; 
    } 

    public int getID() { 
     return id; 
    } 


    public void setID(int id) { 
     this.id = id; 
    } 


    public String getQuestion() { 
     return question; 
    } 


    public void setQuestion(String question) { 
     this.question = question; 
    } 


    public String getAnswer(int i){ 
      return sAnswers.get(i); 

    } 


    public void setAnswer(int i,String answer){  
      sAnswers.add(answer); 
    } 


    //get the correct answer as a string 
    public String sGetCorrectAnswer(){ 
     return sAnswers.get(correctAns); 
    } 


    public String getAnswersList() 
    { 
     String s="\nA) " + sAnswers.get(0) + 
       "\nB) " + sAnswers.get(1) + 
       "\nC) " + sAnswers.get(2) + 
       "\nD) " + sAnswers.get(3); 
     return s; 
    } 


    public int getiCorrectAns() { 
     return correctAns; 
    } 
    public void setCorrectAns(int correctAns) { 
     this.correctAns = correctAns; 
    } 


     @Override 
    public String toString(){ 

     String log ="\n\nQuestion by ID" + 
       "\n\nId: "+this.getID()+ 
       "\nQuestion: " + this.getQuestion() + 
       " \nAns1: " + this.getAnswer(0) + 
       " \nAns2: " + this.getAnswer(1) + 
       " \nAns3: " + this.getAnswer(2) + 
       " \nAns4: " + this.getAnswer(3) + 
       " \nCorrectAns: " + this.getiCorrectAns()+ 
       " \nDifficulty: " + this.getiDifficulty() 
       ; 
     return log; 
    } 



} 
+1

嗯..爲什麼不使用XmlPullParser? –

+1

Sax解析器是讀取xml文件的好方法。它在很多平臺上都受支持,您可以使用它解析vety大型xml文件。 – AlexS

回答

2

startElement()每次找到xml-start-tag時都會調用。這意味着它是爲您提供以下內容(qname的)例如:

  • questionList
  • 問題
  • ID
  • 問題
  • ansArray
  • string
  • correctAns
  • iDifficulty
  • 問題
  • ID
  • 問題
  • ansArray
  • 個correctAns
  • iDifficulty

這意味着你必須在XML數據自己(的水平,元樹)來管理您的位置。

+0

感謝您對正確軌道的幫助,我終於找到了工作。 –