Thanks for the suggestion, but I was aware about that problem. Adding a comment before the root node apparently doesn't break the code at all:
<?xml version="1.0" encoding="UTF-8"?> <!-- this is a comment --> <Example agent="myclient"> <value1>aaa</value1> </Example> this one works fine. Instead of iterating through nodes and testing for xmlTextReaderNodeType(reader) I used the xmlTextReaderCurrentDoc / xmlDocGetRootElement sequence because they should work anyway, no matter how the XML is structured (and the code is much simpler). Am I wrong? I'd be very pleased if you could give me an XML example that you think could really break my code. SM On 10/13/2014 11:32 AM, Daniel Veillard wrote: > On Sat, Oct 11, 2014 at 02:16:31PM +0200, Spartaco wrote: >> Well, >> the simpliest solution I've found on my own is like that: >> >> xml = xmlReaderForFile("example.xml", NULL, 0); >> xmlTextReaderRead(xml); >> doc = xmlTextReaderCurrentDoc(xml); >> root = xmlDocGetRootElement(doc); >> agent = xmlGetProp(root, "agent"); >> ... >> >> and then proceed with the rest of parsing using the Reader interface, as >> usual. >> Do not forget to use xmlFreeDoc(doc) at the end of parsing. > Well that's not 100% guaranteed to work, the xmlTextReaderRead() > will scan through all nodes. If there is another node (not element) > before the root node, like say a comment, you code will break. > do Read() until you reach an element and use the reader > to get the attribute. > > Daniel >> On 10/11/2014 12:12 PM, Spartaco wrote: >>> Hello guys, >>> >>> I'm just wondering if is there a simple way in LibXML2 to get an >>> attribute value from the root node, preferably using the Reader interface. >>> I have no problems at all with attributes from other nodes, they all are >>> accessible by >>> xmlTextReaderGetAttribute function, but root. >>> Apparently LibXML2 discards root node attributes, or may be I'm missing >>> something. >>> >>> Just to be clear, I intend to parse something like: >>> >>> <?xml version="1.0" encoding="UTF-8"?> >>> <Example agent="myclient"> >>> <value1>aaa</value1> >>> </Example> >>> >>> and I'd like to get the value of the "agent" attribute. >>> Can someone give me an hint? >>> >>> Regards >>> SM >>> >>> _______________________________________________ >>> xml mailing list, project page http://xmlsoft.org/ >>> xml@gnome.org >>> https://mail.gnome.org/mailman/listinfo/xml >>> >> _______________________________________________ >> xml mailing list, project page http://xmlsoft.org/ >> xml@gnome.org >> https://mail.gnome.org/mailman/listinfo/xml _______________________________________________ xml mailing list, project page http://xmlsoft.org/ xml@gnome.org https://mail.gnome.org/mailman/listinfo/xml