miniXML解析C API

miniXML解析C API,第1张

概述我正在尝试解析以下 XML文件: <root>Root <pai>Pai_1 <filho>Pai1,Filho1</filho> <filho>Pai1,Filho2</filho> </pai> <pai>Pai_2 <filho>Pai2,Filho1</filho> <filho>Pai2,Filho2 我正在尝试解析以下 XML文件:

<root>Root    <pai>Pai_1        <filho>Pai1,Filho1</filho>        <filho>Pai1,Filho2</filho>    </pai>    <pai>Pai_2        <filho>Pai2,Filho1</filho>        <filho>Pai2,Filho2</filho>    </pai></root>

我正在使用以下C代码:

//... open filexml_tree = mxmlLoadfile(NulL,fp,MXML_TEXT_CALLBACK);node = xml_tree;printf("%s\n",mxmlGetText(node,NulL));// here the return is: Root// I expected: Root,OKnode = xml_tree->child;printf("%s\n",NulL));// here the return is: Root// I expected: Pai_1,not OKnode = mxmlGetFirstChild(xml_tree);printf("%s\n",not OKnode = mxmlFindElement(xml_tree,xml_tree,"pai",NulL,MXML_DESCEND);printf("%s\n",NulL));// here the return is: Pai_1// I expected: Pai_1,OKnode = mxmlGetNextSibling(node);printf("%s\n",NulL));// here the return is: (NulL)// I expected: Pai_2,not OK

我怎样才能访问root的子进程?我的访问概念在哪里错了?

谢谢.

在@RutgersMike回复之后编辑

我扩展你的while循环以尝试理解minixml的概念:

root = mxmlLoadfile(NulL,MXML_TEXT_CALLBACK);node = root;printf("------- Root\n");fprintf(stdout,"Element = %s\n",mxmlGetElement(node));fprintf(stdout,"  Value = %s\n",0));printf("\n");printf("------- First child of Root\n");node = mxmlGetFirstChild(node);fprintf(stdout,0));printf("\n");printf("------- Sibling 1 of First child of Root\n");node = mxmlGetNextSibling(node);fprintf(stdout,0));printf("\n");printf("------- Sibling 2 of First child of Root\n");node = mxmlGetNextSibling(node);fprintf(stdout,0));printf("\n");printf("------- Sibling 3 of First child of Root\n");node = mxmlGetNextSibling(node);fprintf(stdout,0));printf("\n");printf("------- Sibling 4 of First child of Root\n");node = mxmlGetNextSibling(node);fprintf(stdout,0));printf("\n");

结果是:

------- RootElement = root  Value = Root------- First child of RootElement = (null)  Value = Root------- Sibling 1 of First child of RootElement = (null)  Value = ------- Sibling 2 of First child of RootElement = pai  Value = Pai_1------- Sibling 3 of First child of RootElement = (null)  Value = ------- Sibling 4 of First child of RootElement = pai  Value = Pai_2

我觉得这个孩子和父母之间的导航概念有点奇怪.为什么兄弟之间有(空)值?

我正在考虑回到ezxml.

谢谢

解决方法 看起来您想要使用此处描述的迭代函数( http://www.minixml.org/mxml.html#3_7)来获取子节点.

编辑:我写这个迭代通过第一个子节点,它工作正常,使用mxmlGetFirstChild和mxmlGetNextSibling:

<!-- language: c -->mxml_node_t* node = mxmlLoadfile(NulL,f,MXML_TEXT_CALLBACK);while ( node != NulL ){   switch ( mxmlGetType(node) )   {   case MXML_ELEMENT:   {      fprintf(stdout,mxmlGetElement(node));   }   break;   case MXML_TEXT:   {      fprintf(stdout,0));   }   break;   default:   {   }   break;   }   mxml_node_t* next = mxmlGetFirstChild(node);   if ( next != NulL )   {      node = next;   }   else   {      next = mxmlGetNextSibling(node);      if ( next != NulL )      {         node = next;      }      else      {         node = next;         fprintf(stdout,"Done\n");      }   }}

产生输出:

Element = rootValue = RootValue = Element = paiValue = Pai_1Value = Element = filhoValue = Pai1,Filho1

我假设您可以使用其中一个getParent函数进行迭代备份,或者在使用一堆节点指针潜入一个子节点之前保存最后一个节点,如果您想迭代整个文件.请注意,我只处理/打印两种节点类型的数据 – 如果您还需要该信息,您还需要尝试查看其他节点类型包含的内容.

**编辑后更多编辑**

前几天我建议其他人尝试libxml2(http://xmlsoft.org/examples/index.html#xmlReader) – 检查链接. xmlReader示例显示用法.创建一个阅读器并遍历节点非常容易 – 当你点击每个节点时,只需检查它的类型以确定它是你关心的那个(通常是ELEMENT,ATTRIBUTE,TEXT和END_ELEMENT),然后拔出名字或价值.我比mxml更喜欢它.

总结

以上是内存溢出为你收集整理的miniXML解析C API全部内容,希望文章能够帮你解决miniXML解析C API所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1219554.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存