我正在尝试通过Android中的XMLParser读取xml文件。但是在运行时我在组织中收到了一些错误并警告了.org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)。 apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)和com.test.SAXXMLParser.parse(SAXXMLParser.java:34)
这是我的XM:L文件内容
<menu>
<item>
<QNO>49</QNO>
<QUESTIONS>Which declaration of the main method below would allow a class to be started as a standalone program. Select the one correct answer.public class xyz {
public static void main(String args[]) {
for(int i = 0; i < 2; i++) {
for(int j = 2; j>= 0; j--) {
if(i == j) break;
System.out.println("i=" + i + " j="+j);
}
}
}
}</QUESTIONS>
<ANSWER>35,35</ANSWER>
<OPTIONA>35,35</OPTIONA>
<OPTIONB>35,40</OPTIONB>
<OPTIONC>36,32</OPTIONC>
<OPTIOND>None of these</OPTIOND>
<EXPLAINATION>NONE</EXPLAINATION>
<QUESTIONTYPE>JAVA</QUESTIONTYPE>
</item>
<item>
<QNO>48</QNO>
<QUESTIONS>What all gets printed when the following code is compiled and run? Select the three correct answers.</QUESTIONS>
<ANSWER>210</ANSWER>
<OPTIONA>200</OPTIONA>
<OPTIONB>205</OPTIONB>
<OPTIONC>210</OPTIONC>
<OPTIOND>220</OPTIOND>
<EXPLAINATION>NONE</EXPLAINATION>
<QUESTIONTYPE>JAVA</QUESTIONTYPE>
</item>
</menu>
这是我的XMLParser代码
public class SAXXMLParser
{
public SAXXMLParser()
{
}
public static Vector parse(InputStream inputstream) throws SAXException, ParserConfigurationException, IOException
{
Vector vector1;
XMLReader xmlreader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
SAXXMLHandler saxxmlhandler = new SAXXMLHandler();
xmlreader.setContentHandler(saxxmlhandler);
xmlreader.parse(new InputSource(inputstream));
vector1 = saxxmlhandler.getDatabase();
Vector vector = vector1;
System.out.println((new StringBuilder("Vectore Length:::::::")).append(vector.size()).toString());
Test_Demo.Vectore_mquestionDatabaseStructure = vector;
return vector;
}
}
Log cat在line = xmlreader.parse(new InputSource(inputstream))中显示错误; 有人可以帮我解决这个问题。
这是Log Cat信息
09-26 12:10:56.011: W/System.err(350): org.apache.harmony.xml.ExpatParser$ParseException: At line 7, column 24: not well-formed (invalid token)
09-26 12:10:56.024: W/System.err(350): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:520)
09-26 12:10:56.024: W/System.err(350): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
09-26 12:10:56.031: W/System.err(350): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
09-26 12:10:56.031: W/System.err(350): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
09-26 12:10:56.031: W/System.err(350): at com.test.SAXXMLParser.parse(SAXXMLParser.java:34)
09-26 12:10:56.031: W/System.err(350): at com.test.Test_Demo.onCreate(Test_Demo.java:85)
09-26 12:10:56.031: W/System.err(350): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-26 12:10:56.031: W/System.err(350): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-26 12:10:56.041: W/System.err(350): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-26 12:10:56.041: W/System.err(350): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-26 12:10:56.041: W/System.err(350): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-26 12:10:56.041: W/System.err(350): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 12:10:56.051: W/System.err(350): at android.os.Looper.loop(Looper.java:123)
09-26 12:10:56.051: W/System.err(350): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-26 12:10:56.051: W/System.err(350): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 12:10:56.051: W/System.err(350): at java.lang.reflect.Method.invoke(Method.java:507)
09-26 12:10:56.051: W/System.err(350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-26 12:10:56.051: W/System.err(350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-26 12:10:56.061: W/System.err(350): at dalvik.system.NativeStart.main(Native Method)
09-26 12:10:56.071: D/AndroidRuntime(350): Shutting down VM
09-26 12:10:56.071: W/dalvikvm(350): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-26 12:10:56.106: E/AndroidRuntime(350): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
答案 0 :(得分:0)
错误是因为 .. {drum roll} .. 它不是有效的XML。这可以通过XML validation进行简单测试,而无需Android应用程序中止。
通常,它不可能格式良好,因为它包含一个裸<
和>
标记。与HTML中的SCRIPT元素不同,XML不会不神奇地支持CDATA。
应用XML-entity escaping或使用CDATA section。正确的XML编辑器也有助于正确编码。