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());
Related
I have a huge bible data that is in xml format. I am making an android Bible application. But I feel like my data is very huge.
In my research, I read that xml parser parses through the whole file till it gets the tag that it needs. Does anyone know an easier and faster way to parse all the data.
SAX parsing may be appropriate when the data extraction logic is relatively simple and forward only... if you want to have the ease and comfort of traversing the hierarchical structure or XPath, then you are out of luck...
JDOM or DOM have serious memory usage issues...
VTD-XML is a library that spans the use cases too complicated for SAX StAX, and too memory intensive for DOM or JDOM.
While VTD-XML loads everything in memory, the memory footprint is a modest 1.3x~1.5x the size of the XML document, which is 3~5x more efficient than DOM..
It also exports a DOM like cursor API and supports XPath 1.0...
Can SAX Parsers use XPath in Java?
You should use a SAX parser, it's the best way to parse large XML files. For instance you can do this:
File inputFile = new File("input.txt");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
UserHandler userhandler = new UserHandler();
saxParser.parse(inputFile, userhandler);
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 am getting the text message and image from the .net web server by sending the xml parameters.
i need to display these messages and images using lazy list Concept.
I am getting messages by sending the xml parameters and display them in lazy list.
but in the above sample they using url to get the images but i am getting images from xml parameters.
i am getting confusion.can any one please help me how can i display images getting from xml parameters.
thanks in advance.
(let me add comment if any one doe's n't understand my question)
The SAX XML parser is available on Android and can read from any class that implements InputStream. Since URLConnection provides a way to return its underlying InputStream, it seems to me you could do something like the following...
URL xmlSource = new URL("some http path");
UrlConnection xmlConnection = xmlSource.openConnection();
org.xml.sax.InputSource xmlStream = new InputSource(xmlConnection.getInputStream());
I haven't tested this myself and Google is careful to warn that the included Sax implementation comes as-is, but it seems to me a good place to start. There are plenty of resources available online on using InputSource and implementing Sax callbacks.
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.
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.