2016-03-01 62 views
-2

我打算制作一个天气程序,所以我需要解析一个XML文件。如何在Linux上用C语言解析xml文件

我已经安装了libxml的(事实上,它被安装)

不过,我不知道如何解析的数字。

这里是我的XML代码部分:

<tmx>-999.0</tmx> 
<tmn>-999.0</tmn> 
<sky>2</sky> 
<pty>0</pty> 

,我需要在最后一行的数字; 'pty'

非常感谢您的帮助。

+0

HWAT是XML的预期大小,你希望它是一直有效?如果只有一个节点,你真的需要lib xml吗?你能不能扫描输入只是为了找到' x'的事件? – dvhh

+0

我正在使用cron从政府收回它。 –

+0

您的XML代码格式不正确。 (通过xmllint测试) – BLUEPIXY

回答

0

考虑到你可能不需要libXML来提取一些值,只要你不关心输入的有效性,你可以加载文件在内存中(或其中的一部分)。

char* xmlChunk; // your data will be stored inside 
char* tagStartBegin = strstr(xmlChunk,"<pty"); 
char* tagStartEnd = strstr(tagStartBegin,">"); 
char* value = tagStartEnd+1; 
// get the end tag 
char* tagEndBegin = strstr(tagStartEnd,"</pty>"); 
//end the string here 
*tagEndBegin = '\0'; 
// parse your value 
doSomeThing(value); 

您可能需要调整代码为Unicode输入如有