[xml] Reading only the text contents of an element
- From: nkat nkat <justformailers gmail com>
- To: xml gnome org
- Subject: [xml] Reading only the text contents of an element
- Date: Thu, 5 Aug 2010 15:44:08 -0700
Hi,
I am using the following code to read the element contents from an xml file.
I just need the text content:
But in the output I get empty lines for those elements which do not
have text content but have child elements in them.
How do i avoid those blank lines from being printed. (I am not able to
compare it with a single newline since for each element i get many
newlines)
Please help me out.. Ive been trying to figure it out from the past two days.
static void
print_element_names(xmlDocPtr doc,xmlNodePtr a_node)
{
xmlNode *cur_node = NULL;
xmlChar * key;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
printf("Element: %s\n", cur_node->name);
key = xmlNodeListGetString(doc,
cur_node->xmlChildrenNode, 1);
printf("keyword: %s\n", key);
xmlFree(key);
}
print_element_names(cur_node->children);
}
}
int
main(int argc, char **argv)
{
xmlDocPtr doc = NULL;
xmlNodePtr root_element = NULL;
doc = xmlReadFile("sample.xml", NULL, 0);
if (doc == NULL) {
printf("error: could not parse file );
}
root_element = xmlDocGetRootElement(doc);
print_element_names(root_element);
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
.................................................
sample.xml looks like this:
<?xml version="1.0"?>
<story>
<table>
<storyinfo>
<author>John Fleck</author>
<datewritten>June 2, 2002</datewritten>
<keyword>example keyword</keyword>
</storyinfo>
</table>
<body>
<headline>This is the headline</headline>
<para>This is the body text.</para>
</body>
</story>
............................................................
Output:
Element:story
Element:table
Element:storyinfo
Element:
author
Element:datewritten
June 2, 2002
Element:keyword
example keyword
Element:body
Element:headline
This is the headline
Element:para
This is the body text.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]