繁体   English   中英

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

[英]Why can't parse this XML file?

我正在申请英语听力

我需要显示级别,名称,音频文件的名称,测试脚本。 我想从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(数据库文件) :我在这里只写了两个元素

    链接到这里: 这里

下面的代码块永远不会将任何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());

暂无
暂无

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

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