Reading an XML using libxml2

会有一股神秘感。 提交于 2019-12-11 16:22:13

问题


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>

回答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>



来源:https://stackoverflow.com/questions/10741221/reading-an-xml-using-libxml2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!