From: Daniel H. <dh...@ma...> - 2004-06-01 07:36:27
|
Hello everyone, I want to read a XML file and stick into a data structure of my own; I need bits of it across my whole project, so passing around NodeSets seems to be a no-go and not the appropriate option, but maybe I'm wrong. In short my problem looks like this: XML file: <aaa> <bbb> <ccc>bla</ccc> <ddd> <eee>1</eee> <eee>2</eee> ... </ddd> </bbb> <bbb> <ccc>blah</ccc> <ddd> <eee>7</eee> <eee>8</eee> ... </ddd> </bbb> </aaa> data structure: struct t_aaa { std::list<t_bbb> bbb; }; struct t_bbb { std::string ccc; t_ddd ddd; }; struct t_ddd { std::list<int> eee; }; Of course I don't DOM-parse stupid aaa-bbb-files to bug you; the real file is http://protosquared.sf.net/protosquared.conf.xml (and http://protosquared.sf.net/protosquared.conf.xsd if you like) - I need the <datasources>-section to be put into http://protosquared.sf.net/datastructures.hh. After that utterly exhausting talk, now my question: How do I do it? I libxml2-xml-schema-validated it, got the appropriate NodeSet via ns = rootNode->find("//datasource/descendant::*"); and begin iterating over it: for(xmlpp::NodeSet::iterator it=ns.begin();it!=ns.end();++it) The problem is, to know when which ddd-list is finished, to add it to the bbb one. Maybe there's a clever trick and I don't see or maybe my "design" is crap. I even appreciate flames, so thanks alot in advance. Have a nice day, Daniel |
From: Daniel H. <dh...@ma...> - 2004-06-01 08:51:13
|
Am Di, den 01.06.2004 um 9:35 Uhr +0200 schrieb Daniel Holbach: > data structure: > struct t_aaa > { > std::list<t_bbb> bbb; > }; > struct t_bbb > { > std::string ccc; > t_ddd ddd; > }; > struct t_ddd > { > std::list<int> eee; > }; I was being silly, gonna convert the structs into classes. But my general problem stays: when/how do I decide, which object is finished and can be "appended" to the other one? Have a nice day, Daniel |
From: Christophe de V. <cde...@al...> - 2004-06-01 13:42:18
|
Daniel Holbach a écrit : >Hello everyone, > >I want to read a XML file and stick into a data structure of my own; I >need bits of it across my whole project, so passing around NodeSets >seems to be a no-go and not the appropriate option, but maybe I'm wrong. > > What makes you think it's inappropriate ? As long as your structures and your xml tree are organised in a similar way, I'd say that using xpath expressions is a bit overkill. ><snip> >I libxml2-xml-schema-validated it, got the appropriate NodeSet via > ns = rootNode->find("//datasource/descendant::*"); >and begin iterating over it: > for(xmlpp::NodeSet::iterator it=ns.begin();it!=ns.end();++it) > >The problem is, to know when which ddd-list is finished, to add it to >the bbb one. Maybe there's a clever trick and I don't see or maybe my >"design" is crap. I even appreciate flames, so thanks alot in advance. > > Either use the NodeLists, either make more precise xpath expression. Christophe |
From: Daniel H. <dh...@ma...> - 2004-06-02 08:25:15
|
Hello Christophe, hello list :-) first of all, thanks for your answer. Am Di, den 01.06.2004 um 15:44 Uhr +0200 schrieb Christophe de VIENNE: > What makes you think it's inappropriate ? As long as your structures and > your xml tree are organised in a similar way, I'd say that using xpath > expressions is a bit overkill. Maybe you're right, but i didn't know exactly, how to access all the subsequent entries of the <datasource>-tags, I only found the find()-method. I now organized the data structures in another way, but my problem is still the same: it's not easy at all to know when to append a list to the aequivalent structure of the father node. If none of you has an additional hint for me, I'll just try to sort it out somehow. Passing NodeLists will require a rewrite of the methods. > Either use the NodeLists, either make more precise xpath expression. I'll look at the xpath tutorial again. Thanks again for the advices, have a nice day, Daniel |
From: Jonathan W. <co...@co...> - 2004-06-02 09:40:53
|
On Wed, Jun 02, 2004 at 10:25:04AM +0200, Daniel Holbach wrote: > Hello Christophe, hello list :-) > > first of all, thanks for your answer. > > Am Di, den 01.06.2004 um 15:44 Uhr +0200 schrieb Christophe de VIENNE: > > What makes you think it's inappropriate ? As long as your structures and > > your xml tree are organised in a similar way, I'd say that using xpath > > expressions is a bit overkill. > Maybe you're right, but i didn't know exactly, how to access all the > subsequent entries of the <datasource>-tags, I only found the > find()-method. > > I now organized the data structures in another way, but my problem is > still the same: it's not easy at all to know when to append a list to > the aequivalent structure of the father node. Surely if you see <ccc> after a series of <eee> elements you know that the <ddd> parent of the <eee>s has been closed and you should append the list of <eee>s to the parent node, and start parsing the new <ccc>. If I understand you correctly it's simply a matter of keeping some state so you know what the previous element was. A simple std::string to hold the name of the last element seen should be enough. If the current node is one that appears nearer the root of the document than the previous node then you know that the previous node and its parent have been closed. jon -- "It is always the best policy to tell the truth, unless, of course, you are an exceptionally good liar." - Jerome K. Jerome |
From: Dan D. <da...@de...> - 2004-06-03 01:30:31
|
On Tue, 2004-06-01 at 03:35, Daniel Holbach wrote: > I want to read a XML file and stick into a data structure of my own; I Did you see the examples/sax_parser_build_dom ? It shows how to build your own classes derived from xmlpp:Element and to use the SAX parser to construct an application-specific DOM. Then, all of the relationship-handling is inherited from xmlpp/libml2's DOM. It may not be exactly what you want, but it might give you some ideas. |