When parsing an xml file in android, I'm doing like this:
try
{
InputStream is = ...
MyContentHandler ch = new MyContentHandler();
Xml.parse(is, Encoding.UTF_8, ch);
}
catch ...
The problem is that sometimes the file I'm trying to parse is not well-formed.
In my case, undeclared namespaces may be present.
The data I'm interested in is not inside those tags so I could simply ignore it, but I get an exception of unbound prefix not inside the content handler but in the parser itself; this means that if the exception occurs the entire parsing process is interrupted.
Is there a way of using the sax parser ignoring this kind of error (or namespaces at all)?
p.s. I want to avoid loading all the file in memory as a string and strip namespaces out of it, or having to rewrite the file.
I found the solution in another thread.
Instead of using Xml.parse you need to manually instantiate a sax parser through the SAXParserFactory and get a reader.
You can then set the reader features.
Among the available features, one disables namespaces and that does the trick.
Reference -> LINK
Related
I need to know what is the best way to parsing XML file in android, I know there is 3 parser (XMLPullParser, Dom Parser and Sax parser) so whats the different between it and if there any code to do that.
Sax Parser : Simple API of XML Parse node to node, using top-down traversing, parse without storing xml, Faster compared to Dom Manipulating of node like insertion or deletion is allowed. Needs SAXParserFactory
Dom Parser : Document Object Model Stores entire xml in memory before processing, traverse in any direction, Manipulating of node like insertion or deletion is NOT allowed. Needs DocumentBuilderFactory
Pull Parser: It provides more control and speed from the above two.
Android training recommends XMLPullParser.
http://developer.android.com/training/basics/network-ops/xml.html
We recommend XmlPullParser, which is an efficient and maintainable way to parse XML on Android.
They also give some code examples.
I'm building an RSS reader APP, and I've been told to use the XMLPullParser interface.
Here is the block of code I'm working with:
XmlResourceParser parser = context.getResources().getXml(resource);
'Resource' is a an integer with the R.id. integer of the Xml file. This is not an internal XML file, so I don't know how to work around this.
Any ideas? Is the XmlResourceParser the wrong approach for this project? I've seen XMLReaders used with content handlers as well. Can you integrate these technologies together?
Thank you
what is the type of your xml source?
xmlPullParser can be used to parse any xml sources.
It is in my opinion the way to do this. Only problem you may encounter is when the rss feed has empty lines. The xml pullparser of android (api-level 14) jumps to the /channel if this is the case. When implementing the parser try to use the AsyncTask to start the reading of the rss feed.
Success with the implementation.
I'm parsing not so large XML files, about 200KB, in my application. Parser fails systematically when parsing some files.
Symptoms:
I use two types of XML parsers: SAX
(XMLReader) and Pull
(XMLPullParser), both of them fail
at the same place in the file
(3182th byte).
I use InputStream as the input source for parsers.
I trieed to wrap FileInputStream with BufferedInputStream and nothing changed.
I don't know if the problem is in the Parser or in InputStream.
Please help to fix the problem or advise a workaround.
I need to encode an XML document into a format that will pass through an XML parser as a string (i.e. strip tags). I then need to decode it back again and I need to do this on Android. What is the library/class in the Android API that I'm looking for?
Thanks
XmlSerializer is probably what you want. Use it to build the "outer" XML document; you can give its text(...) method an intact XML string and it will do the escaping for you. You can do the same kind of thing with the DOM API by using setTextContent if you really want to build a DOM tree and then dump that to XML. As you appear to be aware, any XML parser will properly decode the entity references when you ask for the text back.
If you really want to write your own XML-building code, you might try pulling in the Apache commons-lang library into your project for it's StringEscapeUtils class. I'm not aware of anything that will just do the entity substitution without building real XML in the base Android API.
For an example, try this:
XmlSerializer xs = Xml.newSerializer();
StringWriter sw = new StringWriter();
xs.setOutput(sw);
xs.startDocument(null, null);
xs.startTag(null, "foo");
xs.text("<bar>this is some complete XML document you had around before</bar>");
xs.endTag(null, "foo");
xs.endDocument();
Log.v("XMLTest", "Generated XML = " + sw.toString());
I ended up using URLEncoder/URLDecoder, which seems to work nicely.
String encoded = URLEncoder.encode(xml);
The general Java XML Parser is the way to go.
and if you have to build it up manually you can use the XmlSerializer
edit:
Here is an article about working with xml on android, but It uses the XmlSerializer for the writing also
this question is one of the first results, when I searched the solution to this problem, so I answer here
Html.fromHtml(String)
I want to test parsing of data returned from server.
I thought I might create several test XML files and then feed them to the sax parser which uses my custom data handler (as in the application that I test).
But where should I put those test XMLs?
I tried with [project_root]/res/xml, but then I get the error:
android.content.res.Resources$NotFoundException: Resource ID #0x7f040000 type #0x1c is not valid
at android.content.res.Resources.loadXmlResourceParser(Resources.java:1870)
at android.content.res.Resources.getXml(Resources.java:779)
Does that mean that the xml is invalid, or that android couldn't find the XML file?
(I use the same XML that comes from the server - copied it from the firebug's net panel and pasted into the file).
Can I somehow read that XML as a text file, since getContext().getResources().getXml(R.xml.test_response1) returns XmlResourceParser instead of String (which I'd prefer)?
Put you xmls in MyAppTest/assets, for example MyAppTest/assets/my.xml, and then read it using
InputStream myxml = getInstrumentation().getContext().getAssets().open("my.xml");
then you can invoke the parser with this stream.
Simply put the file in your current package (ie into a resource package) and use the class loader...
private InputStream getTestImage(String pathToFile) throws IOException
{
final ClassLoader loader = getClass().getClassLoader();
assertNotNull(loader);
final InputStream resourceAsStream = loader.getResourceAsStream(pathToFile);
assertNotNull(resourceAsStream);
return resourceAsStream;
}