You are not logged in.

#1 2009-06-13 11:24:07

ConnorBehan
Package Maintainer (PM)
From: Long Island NY
Registered: 2007-07-05
Posts: 1,359
Website

libxml2 weirdness

I've been trying to use the libxml2 API in C recently but I have noticed something strange. If I use it to create a new XML document and save it with xmlSaveFormatFile, newlines and indentations will be added properly. But if I parse an existing XML document, add nodes to the tree and resave with xmlSaveFormatFile (to a new file or overwriting) everything I added since the last save will all be on one line. This can probably fixed after the fact with a simple hack but does anyone know what's going on?

A test case is here:

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

int main() {
    xmlDocPtr doc;
    xmlNodePtr node;
    xmlNodePtr root_node;
    
    doc = xmlNewDoc("1.0");
    root_node = xmlNewDocNode(doc, NULL, (const xmlChar*) "outer", NULL);
    xmlDocSetRootElement(doc, root_node);
    node = xmlNewTextChild(root_node, NULL, (const xmlChar *) "inner1", NULL);
    xmlNewTextChild(node, NULL, (const xmlChar *) "tag", "hello");
    xmlNewTextChild(node, NULL, (const xmlChar *) "tag", "hello");
    xmlNewTextChild(node, NULL, (const xmlChar *) "tag", "hello");
    
    xmlSaveFormatFile("./test.xml", doc, 1);
    xmlFreeDoc(doc);
    
    doc = xmlParseFile("./test.xml");
    root_node = xmlDocGetRootElement(doc);
    node = xmlNewTextChild(root_node, NULL, (const xmlChar *) "inner2", NULL);
    xmlNewTextChild(node, NULL, (const xmlChar *) "tag", "hello");
    xmlNewTextChild(node, NULL, (const xmlChar *) "tag", "hello");
    xmlNewTextChild(node, NULL, (const xmlChar *) "tag", "hello");
    
    xmlSaveFormatFile("./test.xml", doc, 1);
    xmlFreeDoc(doc);
}

6EA3 F3F3 B908 2632 A9CB E931 D53A 0445 B47A 0DAB
Great things come in tar.xz packages.

Offline

Board footer

Powered by FluxBB