On Mon, Jan 4, 2010 at 8:27 PM, Andreas Wagner <[email protected]>wrote:
>
>
> 2010/1/4 Liam R E Quin <[email protected]>
>
> On Mon, 2010-01-04 at 17:35 +0100, Andreas Wagner wrote:
>>
>> > <param parName="parameter1" type="DOUBLE" val="0.0"/> <----- these
>> > attributes ... but i cant ...strange ... the line above works
>>
>> > do u have an idea what can be wrong?
>>
>> Note that unprefixed attributes are not in any namespace.
>>
>
> I know, but i removed every namespace to test if i can get the attributes
> of the param line, but i cant?
>
If you don't mind that the namespaces don't appear in your nodes then you
can simply parse the document with XML_PARSE_RECOVER | XML_PARSE_NOERROR.
Keep in mind that this will hide other errors in the document and could bite
you later!
cur = cur->xmlChildrenNode;
> if (!xmlStrcmp(cur->name, (const xmlChar*) "param"){
> name = xmlGetProp(cur,(const xmlChar*) "parName");
> }
>
> // the name is never param ... its always text??
> when the current node cur is parameter i try to read the attributes from
> the param line with:
>
> You probably don't have a pointer to the right node, are you sure that
cur->type == XML_ELEMENT_NODE? Perhaps you could use XPath to find the right
nodes.
I used your original document as it is (with missing namespaces) and I was
able to find the right attributes with the sample program attached to this
email.
--
Emmanuel Rodriguez
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
/* Compile with: gcc --std=c99 -o walk walk.c `pkg-config --libs --cflags libxml-2.0` */
static xmlDoc* my_parse_document (const xmlChar *filename) {
xmlParserCtxt *ctxt = NULL;
xmlDoc *document = NULL;
ctxt = xmlCreateFileParserCtxt(filename);
xmlCtxtUseOptions(ctxt, XML_PARSE_COMPACT | XML_PARSE_RECOVER | XML_PARSE_NOERROR);
if (xmlParseDocument(ctxt) == 0) {
document = ctxt->myDoc;
ctxt->myDoc = NULL;
}
xmlFreeParserCtxt(ctxt);
return document;
}
/* Walk the given DOM and show more interrest on <scalar parName="value"> */
void walk (xmlNode *node, int depth) {
if (node->type == XML_ELEMENT_NODE) {
int i = 0;
for (; i < depth; ++i) {
printf(" ");
}
printf("%s ", node->name);
if (xmlStrcmp(node->name, "scalar") == 0) {
xmlChar *value = xmlGetNoNsProp(node, "parName");
printf(" parName = %s", value);
if (value) xmlFree(value);
}
printf("\n");
}
else if (node->type == XML_DOCUMENT_NODE) {
printf("/\n");
}
xmlNode *child = node;
for (child = node->children; child; child = child->next) {
walk(child, depth + 1);
}
}
int
main (int argc, char **argv) {
xmlDoc *document = NULL;
xmlNode *root = NULL;
xmlNode *node = NULL;
const xmlChar *filename = NULL;
if (argc == 1) {
printf("Usage: filename\n");
return 1;
}
filename = (const xmlChar *) argv[1];
document = my_parse_document(filename);
if (document == NULL) {
printf("Failed to parse %s\n", filename);
return 1;
}
walk((xmlNode *) document, 0);
xmlFreeDoc(document);
xmlCleanupParser();
return 0;
}
_______________________________________________
xml mailing list, project page http://xmlsoft.org/
[email protected]
http://mail.gnome.org/mailman/listinfo/xml