How to convert InputStream to InputSource?

ALL,

I wrote a simple SAX XML parser. It works, and I tested it using a local XML file. Here is my code:

SAXParserFactory spf = SAXParserFactory.newInstance(); XMLParser xmlparser = null; try { SAXParser parser = spf.newSAXParser(); XMLReader reader = parser.getXMLReader(); xmlparser = new XMLParser(); reader.setContentHandler( xmlparser ); reader.parse( new InputSource( getResources().openRawResource( R.raw.categories ) ) ); 

Now I need to read this XML file from the website. The code I'm trying to do is:

 public InputStream getXMLFile() { URL url = new URL("http://example.com/test.php?param=0"); InputStream stream = url.openStream(); Document doc = docBuilder.parse(stream); } reader.parse( new Communicator().getXMLFile() ); 

I get a compiler error

"The parse (InputSource) method is not applicable for the argument (InputStream)."

I need help figuring out what I need.

Thanks.

+4
source share
1 answer

While I hate to sound obvious, is there a reason you are not using this constructor ?

 InputSource source = new InputSource(stream); Document doc = docBuilder.parse(source); 

Note that this is very similar to what you do in the first section of code. In the end, openRawResource also returns an InputStream ...

+13
source

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


All Articles