On Mon, Jan 4, 2010 at 12:06 AM, Andreas Wagner <[email protected]>wrote:
>
>
> 2010/1/3 Emmanuel Rodriguez <[email protected]>
>
>>
>>
>> On Sun, Jan 3, 2010 at 9:16 PM, Andreas Wagner
>> <[email protected]>wrote:
>>
>>> Hi Emmanuel,
>>>
>>> thx for ur answer, but it is still the same problem. I must be in the
>>> right node (correct line), but i do not get the attributes :-(
>>
>> Sorry I read your first mail too fast and focused only on the namespace
>> and not on the node!
>>
>> You need to use the URI for nodes that use a namespace. In your case only
>> some elements use namespaces but the attributes don't. Since the attributes
>> have no namespace you will need to use another function. Try to use
>> xmlGetNoNsProp() or xmlGetProp().
>>
>>
>> --
>> Emmanuel Rodriguez
>>
>
> No problem, but i tried the other functions and i just get NULL. Maybe its
> a problem that i work with eclipse under Windows and a static lib. I get one
> warning when i compile my program (but that should just be the xmlfree()
> function). Tomorrow i will try VC 2008.
>
I've attached an simple example that shows how to get attributes from some
nodes.
>
> So i have another question. what shall happen when the current node (cur)
> is a node in this namespace and i say: cout << cur->nsDef->prefix << endl;
> (i expect that the output is the prefix?? or??) My application crashes. And
> i do not know if it is my mistake or a mistake of the enviroment.
>
I think that you want to use cur->ns to get the current namespace. It could
be possible that a node has namespace declarations but that it uses none of
them (it's children might use them).
It's also not a good practice to rely on namespace prefixes. Renaming the
namespace prefixes shouldn't affect an XML parser nor an application as long
as the namespaces refer to the same URI. An XML parser cares that the nodes
namespaces refer to the same URI for an exact match, the value of the prefix
(if there was one) is of no importance to the parser.
--
Emmanuel Rodriguez
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
/* Compile with: gcc -o ns ns.c `pkg-config --libs --cflags libxml-2.0` */
static xmlDoc* my_parse_document (const xmlChar *content) {
xmlParserCtxt *parserCtxt = NULL;
xmlDoc *document = NULL;
parserCtxt = xmlCreateMemoryParserCtxt(content, xmlStrlen(content));
if (xmlParseDocument(parserCtxt) == 0) {
document = parserCtxt->myDoc;
parserCtxt->myDoc = NULL;
}
xmlFreeParserCtxt(parserCtxt);
return document;
}
int
main (int argc, char **argv) {
const xmlChar *ns_uri = "http://dummies.org";
xmlDoc *document = NULL;
xmlChar *value = NULL;
xmlNode *root = NULL;
xmlNode *node = NULL;
document = my_parse_document(
"<root xmlns:Value='http://dummies.org' Value:name='yyy'><Value:scalar name='xxx' type='BOOL'/></root>"
);
if (document == NULL) {
printf("Failed to parse XML\n");
return 1;
}
root = document->children;
printf("Root.name = %s\n", root->name);
value = xmlGetProp(root, "name");
printf("xmlGetProp(root) is %s\n", value);
if (value) xmlFree(value);
value = xmlGetNsProp(root, "name", ns_uri);
printf("xmlGetNsProp(root) is %s\n", value);
if (value) xmlFree(value);
value = xmlGetNoNsProp(root, "name");
printf("xmlGetNoNsProp(root) is %s\n", value);
if (value) xmlFree(value);
printf("\n");
node = root->children;
printf("Node.name = %s\n", node->name);
value = xmlGetProp(node, "name");
printf("xmlGetProp(Value:scalar) = %s\n", value);
if (value) xmlFree(value);
value = xmlGetNsProp(node, "name", ns_uri);
printf("xmlGetNsProp(Value:scalar) = %s\n", value);
if (value) xmlFree(value);
value = xmlGetNoNsProp(node, "name");
printf("xmlGetNoNsProp(Value:scalar) = %s\n", value);
if (value) xmlFree(value);
xmlFreeDoc(document);
xmlCleanupParser();
return 0;
}
_______________________________________________
xml mailing list, project page http://xmlsoft.org/
[email protected]
http://mail.gnome.org/mailman/listinfo/xml