Why can't I open this XML file in SAXParser?

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(); } } 

This is my code. (Many thanks to these guys: Can someone help me with this JAVA SAXParser? )

The problem is that I always fall into IOException e . This post is:

 java.io.IOException: Couldn't open....and then my XML file. 

My XML file is this and the String :

 <?xml version="1.0" encoding="UTF-8"?> <result> <person> <first_name>Mike</first_name> </person> </result> 

Why can't he read my XML string?

+4
source share
2 answers

The parse method that you call expects a URI for the read XML file.

Instead, you can create an InputSource from a StringReader as follows:

 InputSource source = new InputSource(new StringReader(thexml)); saxParser.parse(source, handler); 
+14
source

The SAXParser parse method expects the file name or URI as the first argument, not the XML file read in the line.

+3
source

Source: https://habr.com/ru/post/1301089/


All Articles