Parsing data from a xml file - Dataset

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Parsing data from a xml file - Dataset

Postby immanueln2005 » Wed Feb 18, 2009 2:15 pm

Hi,

I am trying to parse a xml file that contains a dataset, which has the structure below.
<NewDataSet>
<Table>
<UserID>20</UserID>
<Logo>
</Logo>
<LastUpdated></LastUpdated>
</Table>
</NewDataSet>

I am using the code below to save the incoming "Logo's".. but the whole dataset is not being parsed.

The start, length attributes of "End Element" are not right as far as I can tell.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class LogoHandler extends DefaultHandler{
  2.  
  3.  
  4.  
  5.      // ===========================================================
  6.  
  7.      // Fields
  8.  
  9.      // ===========================================================
  10.  
  11.      
  12.  
  13.      private boolean in_NewDataSet = false;
  14.  
  15.      private boolean in_Table= false;
  16.  
  17.      private boolean in_UserID= false;
  18.  
  19.      private boolean in_LastUpdated=false;
  20.  
  21.    
  22.  
  23.      private boolean in_Logo=false;
  24.  
  25.      
  26.  
  27.      Context mContext;
  28.  
  29.      
  30.  
  31.      public LogoHandler(Context ctx)
  32.  
  33.      {
  34.  
  35.          super();
  36.  
  37.          mContext = ctx;
  38.  
  39.      }
  40.  
  41.      
  42.  
  43.      private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet(mContext);
  44.  
  45.  
  46.  
  47.      // ===========================================================
  48.  
  49.      // Getter & Setter
  50.  
  51.      // ===========================================================
  52.  
  53.  
  54.  
  55.      public ParsedExampleDataSet getParsedData() {
  56.  
  57.           return this.myParsedExampleDataSet;
  58.  
  59.      }
  60.  
  61.  
  62.  
  63.      // ===========================================================
  64.  
  65.      // Methods
  66.  
  67.      // ===========================================================
  68.  
  69.      @Override
  70.  
  71.      public void startDocument() throws SAXException {
  72.  
  73.           this.myParsedExampleDataSet = new ParsedExampleDataSet(mContext);
  74.  
  75.      }
  76.  
  77.  
  78.  
  79.      @Override
  80.  
  81.      public void endDocument() throws SAXException {
  82.  
  83.           // Nothing to do
  84.  
  85.      }
  86.  
  87.  
  88.  
  89.      /** Gets be called on opening tags like:
  90.  
  91.       * <tag>
  92.  
  93.       * Can provide attribute(s), when xml was like:
  94.  
  95.       * <tag attribute="attributeValue">*/
  96.  
  97.      @Override
  98.  
  99.      public void startElement(String namespaceURI, String localName,
  100.  
  101.                String qName, Attributes atts) throws SAXException {
  102.  
  103.           if (localName.equals("NewDataSet")) {
  104.  
  105.                this.in_NewDataSet = true;
  106.  
  107.           }else if (localName.equals("Table")) {
  108.  
  109.                this.in_Table = true;
  110.  
  111.           }else if (localName.equals("UserID")) {
  112.  
  113.                this.in_UserID = true;
  114.  
  115.           }
  116.  
  117.             else if(localName.equals("Logo"))  {
  118.  
  119.                 this.in_Logo= true; }
  120.  
  121.             else if(localName.equals("LastUpdated"))  {
  122.  
  123.                 this.in_LastUpdated= true; }
  124.  
  125.            
  126.  
  127.                   }
  128.  
  129.      
  130.  
  131.  
  132.  
  133.              
  134.  
  135.            
  136.  
  137.             //else if (localName.equals("tagwithnumber")) {
  138.  
  139.          // }
  140.  
  141.                // Extract an Attribute
  142.  
  143.               // String attrValue = atts.getValue("thenumber");
  144.  
  145.               // int i = Integer.parseInt(attrValue);
  146.  
  147.               // myParsedExampleDataSet.setExtractedInt(i);
  148.  
  149.         //  }
  150.  
  151.      
  152.  
  153.      
  154.  
  155.      /** Gets be called on closing tags like:
  156.  
  157.       * </tag> */
  158.  
  159.      @Override
  160.  
  161.      public void endElement(String namespaceURI, String localName, String qName)
  162.  
  163.                throws SAXException {
  164.  
  165.           if (localName.equals("NewDataSet")) {
  166.  
  167.                this.in_NewDataSet = false;
  168.  
  169.           }else if (localName.equals("Table")) {
  170.  
  171.                this.in_Table= false;
  172.  
  173.           }else if (localName.equals("UserID")) {
  174.  
  175.                this.in_UserID = false;
  176.  
  177.           }  
  178.  
  179.              
  180.  
  181.           else if(localName.equals("Logo"))   {
  182.  
  183.               this.in_Logo=false; }
  184.  
  185.           else if(localName.equals("LastUpdated"))  {
  186.  
  187.               this.in_LastUpdated= false; }
  188.  
  189.          
  190.  
  191.      }
  192.  
  193.      
  194.  
  195.      /** Gets be called on the following structure:
  196.  
  197.       * <tag>characters</tag> */
  198.  
  199.      @Override
  200.  
  201.     public void characters(char ch[], int start, int length) {
  202.  
  203.           if(this.in_UserID)
  204.  
  205.           {
  206.  
  207.                   myParsedExampleDataSet.setUserID(new String(ch, start, length));
  208.  
  209.           }
  210.  
  211.          
  212.  
  213.           if(this.in_Logo)
  214.  
  215.           {
  216.  
  217.               myParsedExampleDataSet.setLogo(new String(ch, start, length));
  218.  
  219.           }
  220.  
  221.          
  222.  
  223.      }
  224.  
  225. }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4




ParsedExampleDataSet :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public String getUserID ()
  2.  
  3.     {
  4.  
  5.          return UserID ;
  6.  
  7.     }
  8.  
  9.     public void setUserID (String UserID )
  10.  
  11.     {
  12.  
  13.          this.UserID  = UserID ;
  14.  
  15.     }
  16.  
  17.    
  18.  
  19.           public String getLogo()
  20.  
  21.           {
  22.  
  23.               return Logo;
  24.  
  25.           }
  26.  
  27.              public void setLogo(String Logo)
  28.  
  29.              {
  30.  
  31.                  this.Logo=Logo;
  32.  
  33.                  
  34.  
  35.                  // INSERTING INTO DATABASE
  36.  
  37.                  
  38.  
  39.              }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Can some one help me on this?

Waiting for a reply.

Thanks,
Immanuel
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Top

Postby Helios » Mon Feb 23, 2009 5:33 pm

Can you explain better which is the problem?
Helios
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 17, 2009 7:11 am

Postby immanueln2005 » Tue Feb 24, 2009 2:09 pm

Hi,

I am parsing data from a xml file with the format below.

<NewDataSet>
<Table>
<UserID>20</UserID>
<Logo>
</Logo>
<LastUpdated></LastUpdated>
</Table>
</NewDataSet>

Suppose the content under logo is 4000 characters... It doesn't parse all the 4000 characters..
Some problem with the Length I suppose.

However i opted for another way of getting an xml file..

Thanks for replying
Immanuel
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Parsing XML

Postby Helios » Tue Feb 24, 2009 3:35 pm

Hi, I believe your code has no problems. Some time ago I did an app that parsed XML like you do and got the following problem. The XML that I was parsing contained an URL in between a tag. The problem was caused because the URL contained XML's scape characters, like & for example. This caused that every time I was parsing the tag <Url> posting.php?mode=newtopic&f=29 </Url> only the string before the & was saved in the parsed data set. I fix this add a counter in the handler code and modifying some things. I hope this was helpful for you.

Regards,
Helios
Helios
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 17, 2009 7:11 am

Postby immanueln2005 » Tue Mar 03, 2009 1:56 pm

Thanks for the reply..

That isn't the case with me.
I am retrieving a base64 string, with 4000 odd characters :D

Its not taking the entire string.

Regards,
Immanuel
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

XML Parsing

Postby bose pandian » Thu Mar 05, 2009 10:49 am

[Hi.. Immanuel..

we can do the xml parsing so many ways in android.. I did using the W3Cdocumentbuilder..
Below i attached my whole example in the rar..
This is xml parsing xml.

I hope this example is very useful to you..

Regards,
Bose Pandian.C
Last edited by bose pandian on Fri Mar 06, 2009 7:17 am, edited 1 time in total.
bose pandian
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue Dec 30, 2008 3:46 pm
Location: bangalore

Top

Postby immanueln2005 » Thu Mar 05, 2009 11:09 am

Thanks a lot for the code.

I too opted for another way to parse.

Hope the code u gave comes in handy someday :)

Regards,
Immanuel
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Postby bose pandian » Fri Mar 06, 2009 7:16 am

Hi.. Immanuel..

Using the AssetManager, we can parse the local xml file also..
If you want parse the xml file that is present in your project file is also possible...

put your xml file in the assets folder and parse using the same code with AssetManager.
because AssetManager is used to load your xml file from assets folder and give that in into parsing.

Regards,
Bose Pandian.C
bose pandian
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue Dec 30, 2008 3:46 pm
Location: bangalore

xml parsing

Postby bose pandian » Mon Mar 16, 2009 1:48 pm

I'm ready to give the exact code of xml parsing if u are interested
if u r interested, reply me

Regards,
Bose Pandian.C
bose pandian
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue Dec 30, 2008 3:46 pm
Location: bangalore

Postby Disturbed1 » Wed May 06, 2009 12:21 pm

hi

I'm trying to parse a .xml file that I have downloaded to phone memory.
so that file is saved in /data/data/package_name/files/something.xml

when I try to parse it, the parser doesn't seem to see that file, or won't open it or something, but when I give my parser the URL from where I downloaded the file, it works just fine.
the thing is I need that file to on phones memory.
I created that file to be MODE_WORLD_READABLE, so it can be accessed by anyone
can you please help me to solve this, it's very important to me.

thanks in advance

EDIT
I just solved my problem, so no need for help... :)
Disturbed1
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 24, 2009 10:21 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Google Feedfetcher and 8 guests