Visit Our UNIX and Linux User Community


libxml c


 
Thread Tools Search this Thread
Top Forums Programming libxml c
# 1  
Old 01-13-2008
libxml c

I've just started using libxml in c++, so far im able to parse a xml string. now i would like to replace <cms:CMSContent id="leftNav" /> with the string "left" and write the new xml out to cout; any ideas?

string.xml
Code:
<html xmlns:cms="http://www.test.com">
<body>
<cms:CMSContent id="leftNav" />
<cms:CMSContent id="rightNav" />
</body>
</html>

test.cpp - function streamFile(string s);
Code:
static void streamFile(string s) {
    xmlTextReaderPtr reader;
    int ret;
	
    reader = xmlReaderForMemory(s.c_str(),s.size(), NULL,NULL, 0);
    if (reader != NULL) {
        ret = xmlTextReaderRead(reader);
        while (ret == 1) {
            processNode(reader);
            ret = xmlTextReaderRead(reader);
        }
        xmlFreeTextReader(reader);
        if (ret != 0) {
		cout << "Failed to parse";	
        }
    } else {
	cout << "unable to open";

    }
static void
processNode(xmlTextReaderPtr reader) {
    const xmlChar *name, *value;

    name = xmlTextReaderConstName(reader);
    if (name == NULL)
	name = BAD_CAST "--";

    value = xmlTextReaderConstValue(reader);

    printf("%d %d %s %d %d", 
	    xmlTextReaderDepth(reader),
	    xmlTextReaderNodeType(reader),
	    name,
	    xmlTextReaderIsEmptyElement(reader),
	    xmlTextReaderHasValue(reader));
	//xmlTextReaderConstValue(reader));
    if (value == NULL)
	printf("\n");
    else {
        if (xmlStrlen(value) > 40)
            printf(" %.40s...\n", value);
        else
	    printf(" %s\n", value);
    }
}


Last edited by spids; 01-13-2008 at 09:55 PM..
# 2  
Old 01-14-2008
Code:
#include <stdio.h>
#include <regex.h>

main(int argc, char *argv[])
{
    char *str, line[BUFSIZ];
    regex_t re;

    
    str = "<cms:CMSContent id=\"leftNav\" />";
    if (regcomp(&re, str, REG_EXTENDED)) {
        perror(argv[0]);
        exit(1); 
    }
    while (gets(line)) {
        if (regexec(&re, line, (size_t) 0, NULL, 0))
            printf("%s\n", line);
        else
            printf("left\n");
    }
}


Previous Thread | Next Thread
Test Your Knowledge in Computers #784
Difficulty: Easy
The firmware for the TRS-80 Model 100 was developed by Steve Jobs.
True or False?

4 More Discussions You Might Find Interesting

1. Programming

Help in libxml++ for Linux

Hi, Using libxml++-2.6, is it possible to change the xsi:noNamespaceSchemaLocation in the root element node? e.g. from <myxml xsi:noNamespaceSchemaLocation="ABC.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> : : </myxml> to <myxml... (1 Reply)
Discussion started by: tanlccc
1 Replies

2. Shell Programming and Scripting

How to nodetype as "ELEMENT_NODE" using xml::libxml in perl?

Hi, I am using xml::libxml. Here is the code. my $parser = XML::LibXML->new(); my $xmldoc = $parser->parse_file ("file.xml") || die("Could not parse config xml file\n"); my $root = $xmldoc->getDocumentElement()|| die("Could not get Document Element \n"); $id="121313131"; # For example... (2 Replies)
Discussion started by: vanitham
2 Replies

3. Programming

[libxml++] DOmParser get node value

Hi I wrote the follow code: this->domParser = new xmlpp::DomParser(); this->domParser->set_validate(true); this->domParser->parse_file(*this->xmlFileName); this->doc = this->domParser->get_document(); xmlpp::Node* rootNode = this->doc->get_root_node(); xmlpp::Node::NodeList list =... (0 Replies)
Discussion started by: takeo.kikuta
0 Replies

4. Programming

libxml c

i have xml file like this <root> <sree> <cnu value="cprogramming"\> </sree> </root> to retrieve the valuse of cnu element i tried the following code Code: void parseDoc() { xmlDocPtr doc; xmlNodePtr cur; doc = xmlParseFile("/usr/share/1.xml"); if (doc == NULL ) {... (1 Reply)
Discussion started by: phani_sree
1 Replies

Featured Tech Videos