468,257 Members | 1,429 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

Printing XML string With XML tags

Hello all,

I;m a beginner with XML. All I want is to print the XML string with tags. In
the following example, there is function called: xmlNodeGetString. This
function is getting a char* back. The result of this function is the XML
elements of the list without tags (Jaap, Kees, Kris). at the end of this
example a function xmlSaveFormatFile writes a XML file. looking like:

<?xml version="1.0"?>
<LinkedList><Element>Jaap</Element><Element>Kees</Element><Element>Kris</Element></LinkedList>

So what I want to do is writing the lines normally writting in the file on
the screen using printf. So if I do a printf I need to get something like:

<?xml version="1.0"?>
<LinkedList>
<Element>Jaap</Element>
<Element>Kees</Element>
<Element>Kris</Element>
</LinkedList>

I;ve searched for xmlfunctions for this, but I'm failing to find the right
one. Does anyone know how to get a output like above? I want do something
like this:

char *fname ;
for ( ; p ; p = p->next ) {
if (p->type == XML_ELEMENT_NODE) {
fname = (char *)SOMEXMLFUNCTION(.........) }
printf (fname);
}

In the place om SOMEXMLFUNCTION should me a xmlfunction that spits out
things like this:

<Element>Jaap</Element>

------example----
#include <iostream>

#include <libxml/parser.h>

using namespace std ;

int main() {

xmlNode *xNode ;
xmlDocPtr doc ;

doc = xmlNewDoc(BAD_CAST "1.0");

xNode = xmlNewNode(NULL, BAD_CAST "LinkedList");
xmlDocSetRootElement(doc, xNode);

xmlNewChild(xNode,NULL,(xmlChar *)"Element", (xmlChar *)"Jaap" ) ;
xmlNewChild(xNode,NULL,(xmlChar *)"Element", (xmlChar *)"Kees" ) ;
xmlNewChild(xNode,NULL,(xmlChar *)"Element", (xmlChar *)"Kris" ) ;
xmlNode * p = xNode->children ;

char *fname ;
for ( ; p ; p = p->next ) {
if (p->type == XML_ELEMENT_NODE) {
fname = (char *)xmlNodeListGetString(p->doc,
p->xmlChildrenNode, 1);
}

cout << fname << endl ;
}

xmlSaveFormatFile ("LinkedList.xml", doc, 0);
xmlFreeDoc(doc);
}

Jun 27 '08 #1
2 3319
On Apr 23, 5:24 pm, "Joah Senegal" <blo...@hva.nlwrote:
Hello all,

I;m a beginner with XML. All I want is to print the XML string with tags. In
the following example, there is function called: xmlNodeGetString. This
function is getting a char* back. The result of this function is the XML
elements of the list without tags (Jaap, Kees, Kris). at the end of this
example a function xmlSaveFormatFile writes a XML file. looking like:

<?xml version="1.0"?>
<LinkedList><Element>Jaap</Element><Element>Kees</Element><Element>Kris</Element></LinkedList>

So what I want to do is writing the lines normally writting in the file on
the screen using printf. So if I do a printf I need to get something like:

<?xml version="1.0"?>
<LinkedList>
<Element>Jaap</Element>
<Element>Kees</Element>
<Element>Kris</Element>
</LinkedList>

I;ve searched for xmlfunctions for this, but I'm failing to find the right
one. Does anyone know how to get a output like above? I want do something
like this:

char *fname ;
for ( ; p ; p = p->next ) {
if (p->type == XML_ELEMENT_NODE) {
fname = (char *)SOMEXMLFUNCTION(.........) }
printf (fname);
}

In the place om SOMEXMLFUNCTION should me a xmlfunction that spits out
things like this:

<Element>Jaap</Element>

------example----
#include <iostream>

#include <libxml/parser.h>

using namespace std ;

int main() {

xmlNode *xNode ;
xmlDocPtr doc ;

doc = xmlNewDoc(BAD_CAST "1.0");

xNode = xmlNewNode(NULL, BAD_CAST "LinkedList");
xmlDocSetRootElement(doc, xNode);

xmlNewChild(xNode,NULL,(xmlChar *)"Element", (xmlChar *)"Jaap" ) ;
xmlNewChild(xNode,NULL,(xmlChar *)"Element", (xmlChar *)"Kees" ) ;
xmlNewChild(xNode,NULL,(xmlChar *)"Element", (xmlChar *)"Kris" ) ;

xmlNode * p = xNode->children ;

char *fname ;
for ( ; p ; p = p->next ) {
if (p->type == XML_ELEMENT_NODE) {
fname = (char *)xmlNodeListGetString(p->doc,
p->xmlChildrenNode, 1);
}

cout << fname << endl ;
}

xmlSaveFormatFile ("LinkedList.xml", doc, 0);
xmlFreeDoc(doc);

}
There is no standard C++ XML library. You will have to use a third
party library or write your own. Luckily there are several. One
popular one is Xerces. I however, would prefer to write my own. The
details of which could fit in a book and are OT here.

You could easily create a string aroudn another string to wrap <and
</around it, but that is only one form and element can take. what if
it has children? what if it has attributes? etc etc. You get into a
complicated tree structure and lots of decision making.
Jun 27 '08 #2
Christopher <cp***@austin.rr.comwrote in
news:66**********************************@t54g2000 hsg.googlegroups.com:
On Apr 23, 5:24 pm, "Joah Senegal" <blo...@hva.nlwrote:
>Hello all,

I;m a beginner with XML. All I want is to print the XML string with
tags. In the following example, there is function called:
[...]
>>
#include <libxml/parser.h>
This appears to be a question about libxml, which is most probably off-
topic here.
There is no standard C++ XML library. You will have to use a third
party library or write your own. Luckily there are several. One
popular one is Xerces. I however, would prefer to write my own. The
details of which could fit in a book and are OT here.
Indeed, Xerces seems to be an overkill in this case. I am not familiar with
libxml; for studying purposes I would recommend the TinyXML library -
small, readable, usable and quite C++.

HTH
Paavo
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jody Gelowitz | last post: by
2 posts views Thread by Michael G | last post: by
5 posts views Thread by Patrick De Ridder | last post: by
3 posts views Thread by Ahmad Abu-Raddad | last post: by
4 posts views Thread by Rob T | last post: by
3 posts views Thread by John Peterson | last post: by
15 posts views Thread by =?Utf-8?B?cGF0cmlja2RyZA==?= | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.