TIM*_*MEX 4 java xml debugging parsing exception
public static void parseit(String thexml){
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser;
try {
saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
}
public void endElement(String uri, String localName, String qName)throws SAXException {
}
public void characters(char ch[], int start, int length) throws SAXException {
}
};
saxParser.parse(thexml, handler);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
Log.e("e", "e", e);
e.printStackTrace();
}catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码.(非常感谢这些家伙:有人可以帮助我使用这个JAVA SAXParser吗?)
问题是,我总是进入IOException e.消息是这样的:
java.io.IOException: Couldn't open....and then my XML file.
Run Code Online (Sandbox Code Playgroud)
我的XML文件是这样的,它是一个字符串:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<person>
<first_name>Mike</first_name>
</person>
</result>
Run Code Online (Sandbox Code Playgroud)
为什么不能读取我的XML String?
Joa*_*uer 14
parse您调用的方法需要读取XML文件的URI.
您可能想要做的是创建InputSource一个StringReader像这样的:
InputSource source = new InputSource(new StringReader(thexml));
saxParser.parse(source, handler);
Run Code Online (Sandbox Code Playgroud)