Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:36:55+00:00 2026-05-28T05:36:55+00:00

I’m trying to build a DOM tree from a xmlTextReaderPtr . In my final

  • 0

I’m trying to build a DOM tree from a xmlTextReaderPtr. In my final program, it will be used to process a small DOM tree with xslt from a large XML file like I did in java. I cannot find the proper way to work with the namespaces/prefixes (how should I create the elements and the attributes ? ). Here is a very basic C code: I’m building the DOM from the stream and I dump the DOM. How should I modify my code to handle the namespaces/prefixes ? Many thanks !

#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlreader.h>


/* dump XML node */
static void print_element_names(xmlNode * a_node)
    {
        xmlNode *cur_node = NULL;

        for (cur_node = a_node; cur_node; cur_node = xmlNextElementSibling(cur_node))
        {
        if (cur_node->type == XML_ELEMENT_NODE) 
            {
            xmlAttrPtr attr;
                printf("node type: Element, name: %s", cur_node->name);
            if(cur_node->ns!=0 && cur_node->ns->href!=0) printf(" with namespace: %s", cur_node->ns->href);
            printf("\n");
            for(attr = cur_node->properties; NULL != attr; attr = attr->next)
                {
                xmlChar* v=xmlGetProp( cur_node,attr->name);
                printf(" @%s=%s ", attr->name,v);
                xmlFree(v);
                }

            }

        print_element_names(cur_node->children);
        }
    }


int main(int argc,char** argv)
    {
    LIBXML_TEST_VERSION;
    xmlTextReaderPtr reader;
    xmlDocPtr doc = NULL;
    xmlNodePtr current=NULL;
    xmlNsPtr ns=NULL;
    /* read from stdin */
    reader=xmlReaderForFd(fileno(stdin),0,"UTF-8",0);


    for(;;)
        {
        int nodeType;
        int ret = xmlTextReaderRead(reader);
        if(ret<=0) break;
        nodeType=xmlTextReaderNodeType(reader);

        switch(nodeType)
            {
            case XML_READER_TYPE_ELEMENT:
                {
                xmlNsPtr ns=0;
                xmlNodePtr node;
                if(doc==NULL)
                    {
                    doc=xmlNewDoc( BAD_CAST "1.0");
                    }
                if(xmlTextReaderConstNamespaceUri(reader)!=0)
                    {
                    /** how should I handle the attributes' namespaces & prefix here ? */
                    xmlNsPtr ns=xmlSearchNs(doc,current,xmlTextReaderConstNamespaceUri(reader));
                    node=xmlNewNode(ns, xmlTextReaderConstName(reader));
                    if(ns==0)
                        {
                        ns=xmlNewNs(node,
                            xmlTextReaderConstPrefix(reader),
                            xmlTextReaderConstNamespaceUri(reader)
                            );
                        }
                    }
                else
                    {
                    node=xmlNewNode(0, xmlTextReaderConstName(reader));
                    }

                if(current==NULL)
                    {
                    xmlDocSetRootElement(doc,node);
                    }
                else
                    {

                    xmlAddChild(current,node);
                    }

                current=node;

                if(xmlTextReaderIsEmptyElement(reader))
                     {
                     current= current->parent;
                     }


                if(xmlTextReaderHasAttributes(reader))
                     {
                     int i;
                     int n_att=xmlTextReaderAttributeCount(reader);
                     for(i=0;i< n_att;++i)
                     {
                     const xmlChar* k;
                     xmlChar* v;
                     xmlTextReaderMoveToAttributeNo(reader,i);
                     k = xmlTextReaderConstName(reader);
                     v = xmlTextReaderValue(reader);
                    /** how should I handle the attributes' namespaces & prefix here ? */
                     xmlNewProp(node,k, v);
                     xmlFree(v);
                     }
                     xmlTextReaderMoveToElement(reader);
                     }

                break;
                }
             case XML_READER_TYPE_END_ELEMENT:
                 {
                 current= current->parent;
                 break;
                 }
             case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
             case XML_READER_TYPE_TEXT:
                 {
                 const xmlChar* v = xmlTextReaderConstValue(reader);
                 xmlNodePtr node= xmlNewDocText(doc,v);
                 xmlAddChild(current,node);
                 break;
                 }
            default:
                {
                fprintf(stderr,"Ignoring node Type %d\n",nodeType);
                break;
                }
            }
        }
    if(doc!=NULL)
        {
        print_element_names(xmlDocGetRootElement(doc));
        xmlDocDump(stderr,doc);
        xmlFreeDoc(doc);
        }
    xmlFreeTextReader(reader);
    xmlCleanupParser();
    xmlMemoryDump();
    return 0;
    }

and here is my test file:

<?xml version="1.0"?>
<a xmlns="http://urn1.org" xmlns:ns1="http://urn2.org" ns1:test="ok">azdazd
    <b xmlns:ns2="http://urn3.org" xmlns:ns3="http://urn4.org" ns3:test="OK"/>
    azd
    <ns1:b test="ok"/>
    xaz
</a>

Many thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:36:56+00:00Added an answer on May 28, 2026 at 5:36 am

    Ok, I’ve found my erros and how to use the API. Here is the code:

    #include <stdio.h>
    #include <libxml/parser.h>
    #include <libxml/tree.h>
    #include <libxml/xmlreader.h>
    
    #define WHERE fprintf(stderr,"[DEBUG]line:%d\n",__LINE__)
    
    /* dump XML node */
    static void print_element_names(xmlNode * a_node)
        {
            xmlNode *cur_node = NULL;
    
            for (cur_node = a_node; cur_node; cur_node = xmlNextElementSibling(cur_node))
            {
            if (cur_node->type == XML_ELEMENT_NODE) 
                {
                xmlAttrPtr attr;
                printf("node type: Element, name: %s", cur_node->name);
                xmlNsPtr ns=cur_node->nsDef;
                while(ns!=0)
                    {
                    printf(" with namespace: %s %p\n", ns->href,ns->prefix);
                    ns=ns->next;
                    }
    
                printf("\n");
                for(attr = cur_node->properties; NULL != attr; attr = attr->next)
                    {
                    xmlChar* v=xmlGetProp( cur_node,attr->name);
    
            if(attr->ns!=0)
                {
                printf(" with namespace: %s %p\n", attr->ns->href,attr->ns->prefix);
    
                }
                    printf(" @%s=%s ", attr->name,v);
                    xmlFree(v);
                    }
            printf("\n");
                }
    
            print_element_names(cur_node->children);
            }
        }
    
    
    int main(int argc,char** argv)
        {
        LIBXML_TEST_VERSION;
        xmlTextReaderPtr reader;
        xmlDocPtr doc = NULL;
        xmlNodePtr current=NULL;
        /* read from stdin */
        reader=xmlReaderForFd(fileno(stdin),0,"UTF-8",0);
    
    
        for(;;)
            {
            int nodeType;
            int ret = xmlTextReaderRead(reader);
            if(ret<=0) break;
            nodeType=xmlTextReaderNodeType(reader);
    
            switch(nodeType)
                {
                case XML_READER_TYPE_ELEMENT:
                    {
                    xmlNsPtr ns=0;
                    xmlNodePtr node;
                    if(doc==NULL)
                        {
                        doc=xmlNewDoc( BAD_CAST "1.0");
                        }
                    if(xmlTextReaderConstNamespaceUri(reader)!=0)
                        {
                        xmlNsPtr ns=xmlSearchNs(doc,current,xmlTextReaderConstNamespaceUri(reader));
                        node=xmlNewNode(ns, xmlTextReaderConstName(reader));
                        if(ns==0)
                            {
                            WHERE;
                            ns=xmlNewNs(node,
                                xmlTextReaderConstNamespaceUri(reader),
                                xmlTextReaderConstPrefix(reader)
                                );
                            }
                        }
                    else
                        {
                        node=xmlNewNode(0, xmlTextReaderConstName(reader));
                        }
    
                    if(current==NULL)
                        {
                        xmlDocSetRootElement(doc,node);
                        }
                    else
                        {
    
                        xmlAddChild(current,node);
                        }
    
                    current=node;
    
    
    
    
                    if(xmlTextReaderHasAttributes(reader))
                         {
                         int i;
                         int n_att=xmlTextReaderAttributeCount(reader);
                         for(i=0;i< n_att;++i)
                         {
                         const xmlChar* k;
                         xmlChar* v;
                         xmlTextReaderMoveToAttributeNo(reader,i);
                         k = xmlTextReaderConstName(reader);
                         v = xmlTextReaderValue(reader);
                         if(xmlTextReaderConstNamespaceUri(reader)!=0)
                            {
                            if(!xmlStrEqual(xmlTextReaderConstNamespaceUri(reader),BAD_CAST "http://www.w3.org/2000/xmlns/"))
                            {
                        xmlNsPtr ns=xmlSearchNs(doc,current,xmlTextReaderConstNamespaceUri(reader));
                            if(ns==0)
                            {
                            ns=xmlNewNs(node,
                                xmlTextReaderConstNamespaceUri(reader),
                                xmlTextReaderConstPrefix(reader)
                                );
                            }
                        xmlNewNsProp(current,ns,
                            xmlTextReaderConstLocalName(reader)
                            ,
                            v);
                        }
                            }
                         else
                            {
                            xmlNewProp(current,k, v);
                            }
    
    
                         xmlFree(v);
                         }
                         xmlTextReaderMoveToElement(reader);
                         }
            if(xmlTextReaderIsEmptyElement(reader))
                         {
                         current= current->parent;
                         }
                    break;
                    }
                 case XML_READER_TYPE_END_ELEMENT:
                     {
                     current= current->parent;
                     break;
                     }
                 case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
                 case XML_READER_TYPE_TEXT:
                     {
                     const xmlChar* v = xmlTextReaderConstValue(reader);
                     xmlNodePtr node= xmlNewDocText(doc,v);
                     xmlAddChild(current,node);
                     break;
                     }
                default:
                    {
                    fprintf(stderr,"Ignoring node Type %d\n",nodeType);
                    break;
                    }
                }
            }
        if(doc!=NULL)
            {
            print_element_names(xmlDocGetRootElement(doc));
            xmlDocDump(stderr,doc);
            xmlFreeDoc(doc);
            }
        xmlFreeTextReader(reader);
        xmlCleanupParser();
        xmlMemoryDump();
        return 0;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.