I have an XML file /res/raw/myxml.xml
I want to parse it with DOM Parser
I used this documentation but my problem how to read this XML file in local
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = mcontext.getResources(R.raw.myxml);
doc.getDocumentElement().normalize();
I have an error in this line Document doc = mcontext.getResources(R.raw.myxml); in mcontext the error message is mcontext cannot be resolved
I want to parse this document with DOM not with SAX
I try the SAX Parser but with it i have a problem with execution of an XPATH query, for this I changed the parser
getResources().openRawResource(R.raw.myxml);
Use this, it worked for my code but I used this with InputStream. Maybe it also works for your code.
I have an XML file /res/raw/myxml.xml. I want to pase it with DOM Parser
Why? Put the XML in res/xml/ and get at it via getXml() on a Resources object. For compiled XML resources, this will be about 10 times faster than SAX and probably 30-40 times faster than DOM. This will run faster and consume less battery life. Here is a sample project demonstrating this.
I have an error in this line Document doc = mcontext.getResources(R.raw.myxml); in mcontext the error message is mcontext cannot be resolved
That is because you do not have an mcontext variable or data member defined. Newcomers to Java should learn Java before trying to develop in Java for Android.
Related
I am currently trying to write an app (a game) that will, I hope, run on both desktop and Android. The data format I have chosen to use is XML because it's more flexible and powerful than JSON, it performs better than a database and I am familiar with it and like it.
However, I'm beginning to wonder if it's going to actually be possible to use XML.
In addition to plain old XML I'm also using XInclude in my data structures and XPath to locate the relevant nodes. This seems to require that I also validate my XML during parse, which is fine because I had already written the XML Schema when developing the XML in the first place.
I've managed to get everything working on the desktop, however, as soon as I tried to run it on Android it failed. It seems that the XML parser used by default on Android doesn't support XInclude (source).
I've added the following line to my build.gradle:
compile "xerces:xercesImpl:2.12.0"
and it seems to have worked in that the SchemaFactory seems to be resolving to the Xerces one as evidenced by the debugging output:
08-21 14:19:35.488 8254-8332 W/System.err: JAXP: Reading jar:file:/data/app/uk.co.redfruit.gdx.wobbegong-geDT8AfzN-vHBlDA-lE0_g==/base.apk!/META-INF/services/javax.xml.validation.SchemaFactory
JAXP: instantiating org.apache.xerces.jaxp.validation.XMLSchemaFactory
But the DocumentBuilderFactory appears to be using the default Android one because it still throws the error:
java.lang.UnsupportedOperationException: This parser does not support specification "Unknown" version "0.0"
Which is what is always thrown when you call setXIncludeAware on the DocumentBuilderFactory (See above link).
I had assumed that including it in my gradle build file would do the trick and it seems like it partly has, but not enough.
I'm hoping that all crosss platform XML parsing problems will be solved by using the same parser implementation on all platforms, but it doesn't seem as straight forward as that...
Take a look at the javadocs for DocumentBuilderFactory.newInstance()
Use the javax.xml.parsers.DocumentBuilderFactory system property.
I suggest you set the javax.xml.parsers.DocumentBuilderFactory system property to the classname of the xerces DocumentBuilderFactory implementation
I'm guessing it's this is the class you want
eg:
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
doFunkyXmlStuff();
To specify the parser to be used as Xerces-2, I would suggest to pass the parser classname when instantiating the DocumentBuilderFactory as follows:
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl", null);
docFactory.setXIncludeAware(true);
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 cant find anything about it.
I just want to find if a specific row exist in my DOM xml object (and more efficiently then going through all nodes).
In order to do that I need an ID attribute and in order to set an attribute as an ID so I could use getElementById I need to set a schema on my DocumentBuilderFactory.
So.. I built an XSD file in a pretty random spot in my resurces (folder called xml) - is there a standard place to put it in?
and I tried something like this:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaSource = new StreamSource(getClass().getResourceAsStream("online_list_schema.xsd"));
Schema mySchema;
mySchema = factory.newSchema(schemaSource);
dbf.setSchema(mySchema);
yes , I don't really know how to use getResourceAsStream .. (why does it asks for a Sting and not for Int?)
but lets start with the fact that
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
generates an error even if its by its own..
12-05 10:00:27.879: E/AndroidRuntime(996): Caused by:
java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
so my questions are:
where should I put an xml schema in my resources (or- should I put
xml schema in my resources if not what should I do?)
why does SchemaFactory.newInstance gives me an error and how do I
use it correctly
how to load the schema source from my resources and are the other
lines in the code correct?
I've tried every tutorial I could on Google to parse my XML file (you can view it here).
All I want to do with the file in the link above is parse it in an Android app and extract each String's name and string.
Could anyone help out here? I've been through at least 7 or so tutorials and I'm losing all hope right now.
Thank you in advanced.
You can use XmlPullParser for parsing XML.
For e.g. refer to http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
JAXP:
JAXP stands for Java API for xml processing.
It is a specification from w3c.
JAXP is an API from SUN.
using JAXP api, we can process xml document in two mthods.
DOM:
Stores the entire xml document into memory before processing.
It occupies more amount of memory.
It traverse in any direction.
Tree data structure
Steps to work with DOM:
Create documentBuilderFactory
DocumentBuilderFactory factory=
DocumentBuilderFactory.newInstance();
Create DocumentBuilder
DocumentBuilder builder=factory. newDocumentBuilder();
get input stream ClassLoader cls=DomReader.class.getClassLoader();
InputStream is=cls.getResourceAsStream("xml file"); 4. parse xml file and get Document object by calling parse method
on DocumentBuilder object.
Document document=builder.parse(is); 5. Traverse dom tree using document object.SAX:
Simple xml parsing.
It parses node by node
Traversing is from top to bottom
Low memory usage
Back navigation is not possible with sax.
//implementing required handlers
public class SaxParse extends DefaultHandler{ } //new instance of saxParserFactory SAXParserFactory factory=SAXParserFactory.newInstance();
//NEW INSTANCE OF SAX PARSER SAXParser saxparser=factory.newSAXParser(); //Parsing xml document
SAXParser.parse(new File(file to be parsed), new SAXXMLParserImpl());