Re: [xml] Strange behavior



The C program assumes that the "interesting" elements are one level
below the document root. Therefore, the additional level introduced by
<statuses> inhibits the program from reaching the correct elements.

Notice that xmlNodePtr provides access to other xmlNode fields so that
you can check the tag name and decide whether to go down another level.

-W


-----Original Message-----
From: Gabriel Duarte <confusosk8 gmail com>

Hello! I'm new to libxml, following the instructions and reading
examples I
could parse a XML file perfectly, but now I got a problem I can't solve
by
my self.
I got this following XML file:

<?xml version="1.0" encoding="UTF-8"?>
<status>
  <created_at>Sat Jan 02 20:44:54 +0000 2010</created_at>
  <id>7309338854</id>
  <text>TESTE_AGAIN</text>
  <source>&lt;a href=&quot;http://apiwiki.twitter.com/&quot;
rel=&quot;nofollow&quot;&gt;API&lt;/a&gt;</source>
  <truncated>false</truncated>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <favorited>false</favorited>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <user>
    <id>13672792</id>
    <name>Gabriel Duarte</name>
    <screen_name>biiielduarte</screen_name>
    <location>Rio de Janeiro</location>
    <description>Just me! Let's have lots of fun!</description>
    <profile_image_url>
http://a1.twimg.com/profile_images/539180228/mau_normal.png
</profile_image_url>
    <url>http://kinuxlinux.org/gabriel_duarte</url>
    <protected>false</protected>
    <followers_count>92</followers_count>
    <profile_background_color>742E00</profile_background_color>
    <profile_text_color>501E02</profile_text_color>
    <profile_link_color>533117</profile_link_color>
    <profile_sidebar_fill_color>B7957B</profile_sidebar_fill_color>
    <profile_sidebar_border_color>B7957B</profile_sidebar_border_color>
    <friends_count>79</friends_count>
    <created_at>Tue Feb 19 14:16:41 +0000 2008</created_at>
    <favourites_count>2</favourites_count>
    <utc_offset>-10800</utc_offset>
    <time_zone>Brasilia</time_zone>
    <profile_background_image_url>
http://a1.twimg.com/profile_background_images/58350922/bg.jpg
</profile_background_image_url>
    <profile_background_tile>true</profile_background_tile>
    <notifications>false</notifications>
    <geo_enabled>false</geo_enabled>
    <verified>false</verified>
    <following>false</following>
    <statuses_count>830</statuses_count>
  </user>
  <geo/>
</status>

The output is:

id : 13672792 name : Gabriel Duarte screen_name : biiielduarte location
:
Rio de Janeiro description : Just me! Let's have lots of fun!
profile_image_url :
http://a1.twimg.com/profile_images/539180228/mau_normal.png url :
http://kinuxlinux.org/gabriel_duarte protected : false followers_count :
92
profile_background_color : 742E00 profile_text_color : 501E02
profile_link_color : 533117 profile_sidebar_fill_color : B7957B
profile_sidebar_border_color : B7957B friends_count : 79 created_at :
Tue
Feb 19 14:16:41 +0000 2008 favourites_count : 2 utc_offset : -10800
time_zone : Brasilia profile_background_image_url :
http://a1.twimg.com/profile_background_images/58350922/bg.jpgprofile_bac
kground_tile
: true notifications : false geo_enabled : false
verified : false following : false statuses_count : 830

I can parser and print the output perfectly, but when I try to use
another
file:

<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
  <created_at>Sat Jan 02 20:44:54 +0000 2010</created_at>
  <id>7309338854</id>
  <text>TESTE_AGAIN</text>
  <source>&lt;a href=&quot;http://apiwiki.twitter.com/&quot;
rel=&quot;nofollow&quot;&gt;API&lt;/a&gt;</source>
  <truncated>false</truncated>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <favorited>false</favorited>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <user>
    <id>13672792</id>
    <name>Gabriel Duarte</name>
    <screen_name>biiielduarte</screen_name>
    <location>Rio de Janeiro</location>
    <description>Just me! Let's have lots of fun!</description>
    <profile_image_url>
http://a1.twimg.com/profile_images/539180228/mau_normal.png
</profile_image_url>
    <url>http://kinuxlinux.org/gabriel_duarte</url>
    <protected>false</protected>
    <followers_count>92</followers_count>
    <profile_background_color>742E00</profile_background_color>
    <profile_text_color>501E02</profile_text_color>
    <profile_link_color>533117</profile_link_color>
    <profile_sidebar_fill_color>B7957B</profile_sidebar_fill_color>
    <profile_sidebar_border_color>B7957B</profile_sidebar_border_color>
    <friends_count>79</friends_count>
    <created_at>Tue Feb 19 14:16:41 +0000 2008</created_at>
    <favourites_count>2</favourites_count>
    <utc_offset>-10800</utc_offset>
    <time_zone>Brasilia</time_zone>
    <profile_background_image_url>
http://a1.twimg.com/profile_background_images/58350922/bg.jpg
</profile_background_image_url>
    <profile_background_tile>true</profile_background_tile>
    <notifications>false</notifications>
    <geo_enabled>false</geo_enabled>
    <verified>false</verified>
    <following>false</following>
    <statuses_count>830</statuses_count>
  </user>
  <geo/>
</status>
</statuses>

The output is:
created_at : Sat Jan 02 20:44:54 +0000 2010 id : 7309338854 text :
TESTE_AGAIN source : <a href="http://apiwiki.twitter.com/";
rel="nofollow">API</a> truncated : false in_reply_to_status_id : (null)
in_reply_to_user_id : (null) favorited : false in_reply_to_screen_name :
(null) user : geo : (null)



It's almost the same file, only differs at the <statuses> node. I don't
know
whats is happening. I need help to finish my project...

The program that parses these XML files is:

#include <stdio.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

void parseCD(xmlDocPtr doc, xmlNodePtr cur)
{
    xmlChar* content;

    cur = cur->children;

    while(cur != NULL)
    {
        if(cur->type == XML_ELEMENT_NODE)
        {
            content = xmlNodeListGetString(doc, cur->xmlChildrenNode,
1);
            printf("%s : %s\n",cur->name,content);
            xmlFree(content);
            /*free(content);*/   /* windows */
            content = NULL;
            }

            cur = cur->next;
        }

}


int main()
{

    xmlDocPtr doc;
    xmlNodePtr cur;

    doc = xmlParseFile("TwittXML.xml");

    cur = xmlDocGetRootElement(doc);

    cur = cur->children;

    while(cur != NULL)
    {
        if(cur->type == XML_ELEMENT_NODE)
        {
            parseCD(doc,cur);
        }

        cur = cur->next;
    }

    xmlFreeDoc(doc);
    return 0;
}


Thanks!





-- 
Gabriel Duarte
Linux User #471185
Rio de Janeiro - RJ
http://kinuxlinux.org/gabriel_duarte

Phones:
(55) (21) 9463-7760 /*Mobile*/
(55) (21) 2464-9302 /*Home*/
(55) (21) 2529-5080 /*Work*/


-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d- s: a--- C++ UL+++ P L++++ E- W+ N++ o++ K++ w---
O- M- V- PS++ PE++ Y PGP- t++ 5-- X+++ R tv++ b++ DI+ D++
G++ e+ h* r+ y++++
------END GEEK CODE BLOCK------



-- 
Gabriel Duarte
Linux User #471185
Rio de Janeiro - RJ
http://kinuxlinux.org/gabriel_duarte

Phones:
(55) (21) 9463-7760 /*Mobile*/
(55) (21) 2464-9302 /*Home*/
(55) (21) 2529-5080 /*Work*/


-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d- s: a--- C++ UL+++ P L++++ E- W+ N++ o++ K++ w---
O- M- V- PS++ PE++ Y PGP- t++ 5-- X+++ R tv++ b++ DI+ D++
G++ e+ h* r+ y++++
------END GEEK CODE BLOCK------
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.gnome.org/archives/xml/attachments/20100104/1784e624/attach
ment.html>

------------------------------

Message: 3
Date: Mon, 04 Jan 2010 13:44:44 -0500
From: Liam R E Quin <liam holoweb net>
To: Andreas Wagner <andreaswagner7 gmail com>
Cc: xml gnome org
Subject: Re: [xml] namespace problem
Message-ID: <1262630684 2089 20 camel desktop barefootcomputing com>
Content-Type: text/plain; charset="ISO-8859-15"

On Mon, 2010-01-04 at 17:35 +0100, Andreas Wagner wrote:

<param parName="parameter1" type="DOUBLE" val="0.0"/>    <----- these
attributes ... but i cant ...strange ... the line above works

do u have an idea what can be wrong?

Note that unprefixed attributes are not in any namespace.

The problem is that there are namespace (as you can see).
But they are never declared and i can not parse the file correctly.So
you need to fix the file. A possible way to do that would be to make a
DTD with some FIXED attribute values declared, to add the
"xmlns:scenario=..." and so forth.  Another might be to use
entities in a wrapper,
<!DOCTYPE wrapper [
  <!ENTITY include SYSTEM "myfile.xml">
]>
<wrapper>&example;</wrapper>

With libxml, you may also be able to do this with xinclude.

Liam
        


-- 
Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org www.advogato.org



------------------------------

_______________________________________________
xml mailing list
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml


End of xml Digest, Vol 69, Issue 7
**********************************



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]