0

I'm using libxml2.so to parse XML file in C on LINUX platform. I have my XML in the format as mentioned below. I can have any number of accounts in the files. I'm using libxml for the first time after someone suggested me on forum. I'm able to parse the file I had only one account. I donot understand how do I implement if I have more than one account. Anyone implemented such thing before in C,libxml on Linux.

<ACCOUNT>
  <ACCOUNT_NO> 123 </ACCOUNT_NO>
  <NAME> XYZ </XYZ>
  <STATE> GA </STATE>
</ACCOUNT>

<ACCOUNT>
  <ACCOUNT_NO> 223 </ACCOUNT_NO>
  <NAME> ABC </XYZ>
  <STATE> FL </STATE>
</ACCOUNT>
dicaprio
  • 713
  • 2
  • 8
  • 25

1 Answers1

1

per XML-Definition, if you have more < ACCOUNT>s you need a surrounding tag f.e. < ACCOUNTS> around all the < ACCOUNT>-tags.

if you have that, you can go "into" the child, and you can while() over the ->next nodes.

EDITH: i suppose you use the DOM-modell. But if you have many (!) < ACCOUNT>s, you should swith to SAX for memory reasons. DOM builds a complete (M)apping of the (D)ocument to (O)bjects in memory.

In SAX, you build a state machine, which is triggered while the reading of the file/memory is done, for every starting tag and ending tag and data.

EDITH 2: if you have to find a special value you should consider to put the key-value (account_no?) into an attribute like < ACCOUNT no="123"> < NAME>< XYZ> < STATE>FL< /STATE> < /ACCOUNT>

Peter Miehle
  • 5,984
  • 2
  • 38
  • 55
  • +1 .. do you mean i should have something like this `123.. 234` ? – dicaprio May 24 '12 at 16:23
  • One more question I had is, can we validate if we have closing tag , f.e tag have corresponding ? if so which function I can use? – dicaprio May 24 '12 at 16:30
  • if you XMLParseDocument() or ParseMemory(), you get an error, if the file/memory is not valid. If you do it with SAX, your state machine should be able to handle this. – Peter Miehle May 24 '12 at 16:32
  • yep! found this one too..`http://www.jamesh.id.au/articles/libxml-sax/libxml-sax.html#introduction` – dicaprio May 24 '12 at 17:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11706/discussion-between-dicaprio-and-peter-miehle) – dicaprio May 24 '12 at 17:55