Reading xml documents

Put your problem here if it does not fit any of the other categories.

Reading xml documents

Postby Gamby » Sat Dec 15, 2007 6:14 pm

Hi all

the first main question is:
How to read xml documents in Android?

Then, I tried with these lines:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. fIn = openFileInput(intent.getData().getPathLeaf());
  2.  
  3. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  4. DocumentBuilder db = dbf.newDocumentBuilder();
  5. Document doc = db.parse(fIn);
  6. doc.getDocumentElement().normalize();
  7. Log.i(TAG, "Root element " + doc.getDocumentElement().getNodeName());
  8.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


But that code throws a SaxParseException Unexpected end of document....WHY in your opinion?

I've obviously tried with the same xml from a simple test java application and the xml was loaded without problems...

Thanks in advance, best regards!
Gamby
Gamby
Freshman
Freshman
 
Posts: 7
Joined: Mon Nov 26, 2007 3:16 pm
Location: Italy

Top

Postby plusminus » Sat Dec 15, 2007 6:42 pm

Hello Gamby,

please post, what intent.getData().getPathLeaf() is, displayed as a String.
[edit]BrainBug...[/edit]

Regards,
plusminus
Last edited by plusminus on Sun Dec 16, 2007 12:43 pm, edited 1 time in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Gamby » Sat Dec 15, 2007 6:45 pm

Don't worry...that statement is correct.
It returns a file name located in folder "files", then I would like to read the xml document located there.

The topic is this:
READING XML IN ANDROID


Thanks again, best regards.
Gamby
Gamby
Freshman
Freshman
 
Posts: 7
Joined: Mon Nov 26, 2007 3:16 pm
Location: Italy

The solution was...

Postby Gamby » Sun Dec 16, 2007 11:10 am

Hi Gamby,

try with:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. fIn = openFileInput(intent.getData().getPathLeaf());
  3.  
  4.  
  5.  
  6. KXmlParser parser = new KXmlParser();
  7.  
  8. parser.setInput(fIn, "UTF-8");
  9.  
  10.                        
  11.  
  12. Document doc = new Document();
  13.  
  14. doc.parse(parser);
  15.  
  16.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Best regards
Gamby
Gamby
Freshman
Freshman
 
Posts: 7
Joined: Mon Nov 26, 2007 3:16 pm
Location: Italy

Postby plusminus » Sun Dec 16, 2007 12:45 pm

Hey Gamby,

didn't know what I was thinking with my post above...

You answered yourself, so it is working :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby plusminus » Sun Dec 16, 2007 7:40 pm

Hello guys,

found this solution on the GoogleGroups (untested :!:)

1.) Put your xml file in res/xml

Code: Select all
res/xml/sample.xml


2.) getXml returns a XmlPullParser object (similar to sax parsers)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  XmlPullParser xpp = ctx.getResources().getXml(R.xml.sample);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


3) parse your xml

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int eventType = xpp.getEventType();
  2.  
  3. while (eventType != XmlPullParser.END_DOCUMENT) {
  4.     if(eventType == XmlPullParser.START_TAG) {
  5.         // parse stuff
  6.     } else if (eventType == XmlPullParser.END_TAG) {
  7.         // parse more stuff
  8.     }
  9.     eventType = xpp.next();
  10. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

hi can i get full source

Postby abhilash » Tue Mar 11, 2008 5:32 am

hi
can i get full source..........of the above discussion
abhilash
Developer
Developer
 
Posts: 26
Joined: Fri Feb 22, 2008 11:21 am
Location: HYDERABAD

Postby plusminus » Tue Mar 11, 2008 9:44 am

Hello abilash,

I suggest working with a :arrow: :src: SAXParser.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby myster23 » Mon Aug 10, 2009 9:06 am

It shows: Error: Binary XML file line#-1:expected start or end tag (position:Binary XML file line #-1)
What is the problem? How can I fix it?

ParseRes.java--->

Code: Select all
try {
        XmlResourceParser xpp=getResources().getXml(R.xml.myxml)
       
        int eventType=xpp.getEventType();
          eventType=xpp.nextTag();
       while(eventType!=XmlResourceParser.END_DOCUMENT)
      {    
         if(xpp.getName().equals("name"))
         {   
            tx1.setText("YES!");
         }
         else { tx1.setText("NO!");  }
          
        eventType=xpp.nextTag();
      }
     this.setContentView(tx1);
   }catch (Exception e) {
      // TODO Auto-generated catch block
   tvx.setText("Error: " + e.getMessage( ));
        Log.e(MY_DEBUG_TAG, "WeatherQueryError", e);
      this.setContentView(tvx);
   }


R.java--->

package com.android.Parsing;

Code: Select all
public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f050001;
        public static final int hello=0x7f050000;
    }
    public static final class xml {
        public static final int myxml=0x7f040000;
    }
}

The xml file I parse is this:
myxml.xml--->

Code: Select all
<?xml version="1.0" encoding="utf-8" ?>
<Person>
<name>Bill</name>
<last>Gates</last>
</Person>


or which is the best way to work with the above way?
myster23
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Apr 23, 2009 7:53 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests