为什么无法解析此XML文件?

时间:2015-08-05 14:22:41

标签: java android xml android-studio

我正在制作英语听力申请

我需要显示音频文件的级别,名称,名称,测试脚本。我想从XML文件中读取信息:"dataeasy.xml"

但我甚至可以阅读dataeasy.xml

的第一个元素

以下是我与之相比:

  • activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayoutXML1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#66CCFF"
    android:orientation="vertical" >
    
    
    <TextView
    android:id="@+id/textViewLessonLevel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:textColor="#000000"
    android:textStyle="bold"
    android:text="Lesson Level"
    android:layout_gravity="center_horizontal"/>
    
    
    <TextView
    android:id="@+id/textViewLessonName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:textColor="#7f7f7f"
    android:textStyle="bold"
    android:text="Lesson Name"
    android:layout_gravity="center_horizontal" />
    
    <TextView
    android:id="@+id/textViewLessonLinkFile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="#865521"
    android:textStyle="bold"
    android:text="Lesson LinkFile"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp" />
    
    <TextView
    android:id="@+id/textViewLessonScript"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="#865521"
    android:textStyle="bold"
    android:text="Lesson Scipt"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp" />
    
    
    
    </LinearLayout>
    
  • DataEasyXML.java

     public class DataEasyXML extends DefaultHandler {
    
     boolean currentElement = false;
     String currentValue = "";
    
     // Array of lessons
     LessonEasyInfo lessonEasyInfo;
     ArrayList<LessonEasyInfo> List;
    
    
     public ArrayList<LessonEasyInfo> getList() {
         return List;
     }
    
     public void startElement(String uri, String localName, String qName,
                         Attributes attributes) throws SAXException {
    
         currentElement = true;
    
         if (qName.equals("Data")) {
             List = new ArrayList<LessonEasyInfo>();
         } else if (qName.equals("Lesson")) {
             lessonEasyInfo = new LessonEasyInfo();
         }
     }
    
     public void endElement(String uri, String localName, String qName)
             throws SAXException {
    
         currentElement = false;
    
         if (qName.equalsIgnoreCase("Level"))
             lessonEasyInfo.setLevel(currentValue.trim());
         else if (qName.equalsIgnoreCase("Name"))
             lessonEasyInfo.setName(currentValue.trim());
         else if (qName.equalsIgnoreCase("LinkFile"))
             lessonEasyInfo.setLinkFile(currentValue.trim());
         else if (qName.equalsIgnoreCase("Script"))
             lessonEasyInfo.setScript(currentValue.trim());
         else if (qName.equalsIgnoreCase("Level"))
             List.add(lessonEasyInfo);
    
         currentValue = "";
     }
    
     public void characters(char[] ch, int start, int length)
             throws SAXException {
    
         if (currentElement) {
             currentValue = currentValue + new String(ch, start, length);
         }
     }
     }
    
  • LessonEasyInfo.java

     public class LessonEasyInfo {
    
     String level = null;
     String name = "";
     String link = "";
     String script = "";
    
     public String getLevel() {
         return level;
     }
    
     public void setLevel(String level) {
         this.level = level;
     }
    
     public String getName() {
         return name;
     }
    
     public void setName(String name) {
         this.name = name;
     }
    
     public String getLinkFile() {
         return link;
     }
    
     public void setLinkFile(String link) {
        this.link = link;
     }
    
     public String getScript() {
         return script;
     }
    
     public void setScript(String script) {
         this.script = script;
     }
    
  • MainActivity.java

     public class MainActivity extends Activity {
    
     TextView tvLessonLevel;
     TextView tvLessonName;
     TextView tvLessonLinkFile;
     TextView tvLessonScript;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
    
         tvLessonLevel = (TextView)findViewById(R.id.textViewLessonLevel);
         tvLessonName = (TextView)findViewById(R.id.textViewLessonName);
         tvLessonLinkFile = (TextView)findViewById(R.id.textViewLessonLinkFile);
         tvLessonScript = (TextView)findViewById(R.id.textViewLessonScript);
    
    
    
         parsedataeasyXML();
     }
    
     private void parsedataeasyXML(){
         // 
         AssetManager assetManager = getBaseContext().getAssets();
         try {
             //Get "dataeasy.xml"
             InputStream is = assetManager.open("dataeasy.xml");
             //
             SAXParserFactory spf = SAXParserFactory.newInstance();
             SAXParser sp = spf.newSAXParser();
             //Read XML Object
             XMLReader xr = sp.getXMLReader();
    
             // My
             DataEasyXML myXMLHandler = new DataEasyXML();
             //
             xr.setContentHandler(myXMLHandler);
             //Input
             InputSource inStream = new InputSource(is);
    
             xr.parse(inStream);
    
             // get the first element
             ArrayList<LessonEasyInfo> List = myXMLHandler.getList();
    
            tvLessonLevel.setText("Level: " + List.get(0).getLevel());
    
    
                 tvLessonName.setText("Name: " + List.get(0).getName());
    
    
                 tvLessonLinkFile.setText("LinkFile : " + List.get(0).getLinkFile());
    
                 tvLessonScript.setText("Script " + List.get(0).getScript());
    
    
    
             is.close();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
    

    }

  • dataeasy.xml(数据库文件):我这里只写2个元素

    链接到这里:here

1 个答案:

答案 0 :(得分:1)

以下代码块永远不会将任何LessonEasyInfo个对象添加到列表中,因为如果第一个if表达式求值为true,则其他任何一个都不会被评估。

 if (qName.equalsIgnoreCase("Level"))
     lessonEasyInfo.setLevel(currentValue.trim());
 else if (qName.equalsIgnoreCase("Name"))
     lessonEasyInfo.setName(currentValue.trim());
 else if (qName.equalsIgnoreCase("LinkFile"))
     lessonEasyInfo.setLinkFile(currentValue.trim());
 else if (qName.equalsIgnoreCase("Script"))
     lessonEasyInfo.setScript(currentValue.trim());
 else if (qName.equalsIgnoreCase("Level"))
     List.add(lessonEasyInfo);

将上述代码更改为此

 if (qName.equalsIgnoreCase("Level")) {
     lessonEasyInfo.setLevel(currentValue.trim());
     List.add(lessonEasyInfo);
 }
 else if (qName.equalsIgnoreCase("Name"))
     lessonEasyInfo.setName(currentValue.trim());
 else if (qName.equalsIgnoreCase("LinkFile"))
     lessonEasyInfo.setLinkFile(currentValue.trim());
 else if (qName.equalsIgnoreCase("Script"))
     lessonEasyInfo.setScript(currentValue.trim());