1

I have a situation, where I get a string representation of an XML and I would like to parse it using SAX parser.

I know there is a way to use document builder in order to get a DOM document and parse it using DOM parser, but I would like to parse it using SAX.

I don't want to save it to a file and read it using the SAX parser, unless this is the only option.

What should I do in this situation?

1 Answer 1

5

Use SAXParser.parse(InputSource is, DefaultHandler dh) where

 InputSource is = new InputSource(new StringReader(str));
Sign up to request clarification or add additional context in comments.

4 Comments

how would a method that gets the string as an argument look like? what will parse return? can you please edit and write it as a method?
something like: public static Document loadXML(String xml) throws Exception { DocumentBuilderFactory fctr = DocumentBuilderFactory.newInstance(); DocumentBuilder bldr = fctr.newDocumentBuilder(); InputSource insrc = new InputSource(new StringReader(xml)); return bldr.parse(insrc); }
sorry but it didn't help.. I'm new to this so any chance you can elaborate?
SAXParserFactory.newInstance().newSAXParser().parse(inputSource, defaultHandler); the processing of events happens in DefaultHandler, you need to write it yourself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.