[android-developers] Re: parsing a *string* of XML with SAX.
I think it's better DOM than SAX in your case, why?, you had it on a String, so it is in memory (the information), DOM read everything and put it in memory, so, you can put in memory and then free the String memory. To do it is so easy: 1- Create a StringBufferInputStream with your String 2- Create a DocumentBuilder (with the DocumentBuilderFactory class) 3- Parse your StringBufferInputStream with your DocumentBuilder Luck!!! On 18 dic, 19:50, redders redders6...@googlemail.com wrote: Okay, so I know that the parse() method takes an inputsource, and normally it'd be better to parse the xml at the source (where it's a stream), but in this case I want to be parsing it on a different thread to the thread it's being received on. My question is basically a why won't this work question... As far as I can tell, the startElement method is never being called (no startelement entries in logcat after trying to parse..) .. any help appreciated. Here's the code. public class NWCommsXMLParser extends DefaultHandler{ public void startElement(String uri, String name, String qName, Attributes atts){ Log.i(NWXML,startelement: + uri + . + name + . + qName + / + atts.toString()); } public void endElement(String uri, String name, String qName){ } public void characters(char ch[], int start, int length) { } public void parseMessage(String xmlMessage){ Log.i(NWXML,Trying to parse: + xmlMessage); try{ SAXParserFactory parseFactory = SAXParserFactory.newInstance(); SAXParser parser = parseFactory.newSAXParser(); XMLReader xmlReader = parser.getXMLReader(); xmlReader.setContentHandler(this); StringReader sr = new StringReader(xmlMessage); InputSource is = new InputSource(sr); xmlReader.parse(is); } catch(Exception e){ Log.e(NanoWarsXML,SAX error: + e); } } } Logcat line before the try{} : 12-19 01:31:38.763: INFO/NWXML(3218): Trying to parse: NWCNWR type=dat id=1 PSL123447/PSLPSLg87645/PSLg/ NWR/NWC -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: parsing a *string* of XML with SAX.
I appreciate the reply, but I selected the SAX method because of its efficiency advantages, so I'd really like to understand why my code isn't working. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: parsing a *string* of XML with SAX.
redders wrote: I appreciate the reply, but I selected the SAX method because of its efficiency advantages, so I'd really like to understand why my code isn't working. I don't use XMLReader and I have no problems with SAX on Android, such as this one parsing a XML payload from a REST request: BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream())); SAXParserFactory f=SAXParserFactory.newInstance(); SAXParser p=f.newSAXParser(); SearchParser parser=new SearchParser(null); p.parse(new InputSource(in), parser); (where SearchParser is a DefaultHandler subclass) In your case, this would look like: SAXParserFactory f=SAXParserFactory.newInstance(); SAXParser p=f.newSAXParser(); StringReader sr = new StringReader(xmlMessage); InputSource is = new InputSource(sr); p.parse(is, this); Try something like that and see if it helps. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://twitter.com/commonsguy _Android Programming Tutorials_ Version 1.0 In Print! -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: parsing a *string* of XML with SAX.
Thanks Mark, I've just given that a try. I still don't get any calls to my startElement method, but there is a new warning which may be a clue: 12-19 17:14:24.241: INFO/NWXML(1852): Trying to parse: *snip* 12-19 17:14:24.241: WARN/ExpatReader(1852): DTD handlers aren't supported. DTD handlers aren't supported. Know what this means? 2009/12/19 Mark Murphy mmur...@commonsware.com redders wrote: I appreciate the reply, but I selected the SAX method because of its efficiency advantages, so I'd really like to understand why my code isn't working. I don't use XMLReader and I have no problems with SAX on Android, such as this one parsing a XML payload from a REST request: BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream())); SAXParserFactory f=SAXParserFactory.newInstance(); SAXParser p=f.newSAXParser(); SearchParser parser=new SearchParser(null); p.parse(new InputSource(in), parser); (where SearchParser is a DefaultHandler subclass) In your case, this would look like: SAXParserFactory f=SAXParserFactory.newInstance(); SAXParser p=f.newSAXParser(); StringReader sr = new StringReader(xmlMessage); InputSource is = new InputSource(sr); p.parse(is, this); Try something like that and see if it helps. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://twitter.com/commonsguy _Android Programming Tutorials_ Version 1.0 In Print! -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: parsing a *string* of XML with SAX.
Update: Using Mark's method, startDocument and endDocument are invoked, but never the elements... On Dec 19, 5:19 pm, Edward Hinchliffe redders6...@googlemail.com wrote: Thanks Mark, I've just given that a try. I still don't get any calls to my startElement method, but there is a new warning which may be a clue: 12-19 17:14:24.241: INFO/NWXML(1852): Trying to parse: *snip* 12-19 17:14:24.241: WARN/ExpatReader(1852): DTD handlers aren't supported. DTD handlers aren't supported. Know what this means? 2009/12/19 Mark Murphy mmur...@commonsware.com redders wrote: I appreciate the reply, but I selected the SAX method because of its efficiency advantages, so I'd really like to understand why my code isn't working. I don't use XMLReader and I have no problems with SAX on Android, such as this one parsing a XML payload from a REST request: BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream())); SAXParserFactory f=SAXParserFactory.newInstance(); SAXParser p=f.newSAXParser(); SearchParser parser=new SearchParser(null); p.parse(new InputSource(in), parser); (where SearchParser is a DefaultHandler subclass) In your case, this would look like: SAXParserFactory f=SAXParserFactory.newInstance(); SAXParser p=f.newSAXParser(); StringReader sr = new StringReader(xmlMessage); InputSource is = new InputSource(sr); p.parse(is, this); Try something like that and see if it helps. -- Mark Murphy (a Commons Guy) http://commonsware.com|http://twitter.com/commonsguy _Android Programming Tutorials_ Version 1.0 In Print! -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2Bunsubs cr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: parsing a *string* of XML with SAX.
In fact it seems that startDocment and endDocument are invoked when using my original code, with no warning about DTD handlers... so how do i get the other methods to be invoked?? 2009/12/19 redders redders6...@googlemail.com Update: Using Mark's method, startDocument and endDocument are invoked, but never the elements... On Dec 19, 5:19 pm, Edward Hinchliffe redders6...@googlemail.com wrote: Thanks Mark, I've just given that a try. I still don't get any calls to my startElement method, but there is a new warning which may be a clue: 12-19 17:14:24.241: INFO/NWXML(1852): Trying to parse: *snip* 12-19 17:14:24.241: WARN/ExpatReader(1852): DTD handlers aren't supported. DTD handlers aren't supported. Know what this means? 2009/12/19 Mark Murphy mmur...@commonsware.com redders wrote: I appreciate the reply, but I selected the SAX method because of its efficiency advantages, so I'd really like to understand why my code isn't working. I don't use XMLReader and I have no problems with SAX on Android, such as this one parsing a XML payload from a REST request: BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream())); SAXParserFactory f=SAXParserFactory.newInstance(); SAXParser p=f.newSAXParser(); SearchParser parser=new SearchParser(null); p.parse(new InputSource(in), parser); (where SearchParser is a DefaultHandler subclass) In your case, this would look like: SAXParserFactory f=SAXParserFactory.newInstance(); SAXParser p=f.newSAXParser(); StringReader sr = new StringReader(xmlMessage); InputSource is = new InputSource(sr); p.parse(is, this); Try something like that and see if it helps. -- Mark Murphy (a Commons Guy) http://commonsware.com|http://twitter.com/commonsguy _Android Programming Tutorials_ Version 1.0 In Print! -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.comandroid-developers%2Bunsubs cr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: parsing a *string* of XML with SAX.
Sorted, for anyone who was interested, the startElement and endElement methods need to look like this: public void endElement(String uri, String localName, String qName)throws SAXException{ Log.i(###,start element); } 2009/12/19 Edward Hinchliffe redders6...@googlemail.com In fact it seems that startDocment and endDocument are invoked when using my original code, with no warning about DTD handlers... so how do i get the other methods to be invoked?? 2009/12/19 redders redders6...@googlemail.com Update: Using Mark's method, startDocument and endDocument are invoked, but never the elements... On Dec 19, 5:19 pm, Edward Hinchliffe redders6...@googlemail.com wrote: Thanks Mark, I've just given that a try. I still don't get any calls to my startElement method, but there is a new warning which may be a clue: 12-19 17:14:24.241: INFO/NWXML(1852): Trying to parse: *snip* 12-19 17:14:24.241: WARN/ExpatReader(1852): DTD handlers aren't supported. DTD handlers aren't supported. Know what this means? 2009/12/19 Mark Murphy mmur...@commonsware.com redders wrote: I appreciate the reply, but I selected the SAX method because of its efficiency advantages, so I'd really like to understand why my code isn't working. I don't use XMLReader and I have no problems with SAX on Android, such as this one parsing a XML payload from a REST request: BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream())); SAXParserFactory f=SAXParserFactory.newInstance(); SAXParser p=f.newSAXParser(); SearchParser parser=new SearchParser(null); p.parse(new InputSource(in), parser); (where SearchParser is a DefaultHandler subclass) In your case, this would look like: SAXParserFactory f=SAXParserFactory.newInstance(); SAXParser p=f.newSAXParser(); StringReader sr = new StringReader(xmlMessage); InputSource is = new InputSource(sr); p.parse(is, this); Try something like that and see if it helps. -- Mark Murphy (a Commons Guy) http://commonsware.com|http://twitter.com/commonsguy _Android Programming Tutorials_ Version 1.0 In Print! -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.comandroid-developers%2Bunsubs cr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: parsing a *string* of XML with SAX.
ok, answered my own question again.. me failing to use eclipse properly: import java.util.jar.Attributes; was at the top, rather than the SAX attributes object. hence my startelement method had the wrong arguments. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: parsing a *string* of XML with SAX.
Worth stating, that try/catch doesn't catch any exceptions, so it isn't arguing with me. On Dec 19, 1:50 am, redders redders6...@googlemail.com wrote: Okay, so I know that the parse() method takes an inputsource, and normally it'd be better to parse the xml at the source (where it's a stream), but in this case I want to be parsing it on a different thread to the thread it's being received on. My question is basically a why won't this work question... As far as I can tell, the startElement method is never being called (no startelement entries in logcat after trying to parse..) .. any help appreciated. Here's the code. public class NWCommsXMLParser extends DefaultHandler{ public void startElement(String uri, String name, String qName, Attributes atts){ Log.i(NWXML,startelement: + uri + . + name + . + qName + / + atts.toString()); } public void endElement(String uri, String name, String qName){ } public void characters(char ch[], int start, int length) { } public void parseMessage(String xmlMessage){ Log.i(NWXML,Trying to parse: + xmlMessage); try{ SAXParserFactory parseFactory = SAXParserFactory.newInstance(); SAXParser parser = parseFactory.newSAXParser(); XMLReader xmlReader = parser.getXMLReader(); xmlReader.setContentHandler(this); StringReader sr = new StringReader(xmlMessage); InputSource is = new InputSource(sr); xmlReader.parse(is); } catch(Exception e){ Log.e(NanoWarsXML,SAX error: + e); } } } Logcat line before the try{} : 12-19 01:31:38.763: INFO/NWXML(3218): Trying to parse: NWCNWR type=dat id=1 PSL123447/PSLPSLg87645/PSLg/ NWR/NWC -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en