开发者

parse an xml file (with DOM parser) in android

开发者 https://www.devze.com 2023-02-21 06:41 出处:网络
I have an XML file /res/raw/myxml.xml I want to parse it with DOM Parser I used this documentationbut my problem how to read this XML file in local

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 que开发者_StackOverflowry, 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消