加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

miniXML解析C API

发布时间:2020-12-16 10:21:36 所属栏目:百科 来源:网络整理
导读:我正在尝试解析以下 XML文件: rootRoot paiPai_1 filhoPai1,Filho1/filho filhoPai1,Filho2/filho /pai paiPai_2 filhoPai2,Filho1/filho filhoPai2,Filho2/filho /pai/root 我正在使用以下C代码: //... open filexml_tree = mxmlLoadFile(NULL,fp,MXML_TE
我正在尝试解析以下 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 file
xml_tree = mxmlLoadFile(NULL,fp,MXML_TEXT_CALLBACK);

node = xml_tree;
printf("%sn",mxmlGetText(node,NULL));
// here the return is: Root
// I expected: Root,OK

node = xml_tree->child;
printf("%sn",NULL));
// here the return is: Root
// I expected: Pai_1,not OK

node = mxmlGetFirstChild(xml_tree);
printf("%sn",not OK

node = mxmlFindElement(xml_tree,xml_tree,"pai",NULL,MXML_DESCEND);
printf("%sn",NULL));
// here the return is: Pai_1
// I expected: Pai_1,OK

node = mxmlGetNextSibling(node);
printf("%sn",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("------- Rootn");
fprintf(stdout,"Element = %sn",mxmlGetElement(node));
fprintf(stdout,"  Value = %sn",0));
printf("n");

printf("------- First child of Rootn");
node = mxmlGetFirstChild(node);
fprintf(stdout,0));
printf("n");

printf("------- Sibling 1 of First child of Rootn");
node = mxmlGetNextSibling(node);
fprintf(stdout,0));
printf("n");

printf("------- Sibling 2 of First child of Rootn");
node = mxmlGetNextSibling(node);
fprintf(stdout,0));
printf("n");

printf("------- Sibling 3 of First child of Rootn");
node = mxmlGetNextSibling(node);
fprintf(stdout,0));
printf("n");

printf("------- Sibling 4 of First child of Rootn");
node = mxmlGetNextSibling(node);
fprintf(stdout,0));
printf("n");

结果是:

------- Root
Element = root
  Value = Root

------- First child of Root
Element = (null)
  Value = Root

------- Sibling 1 of First child of Root
Element = (null)
  Value = 

------- Sibling 2 of First child of Root
Element = pai
  Value = Pai_1

------- Sibling 3 of First child of Root
Element = (null)
  Value = 

------- Sibling 4 of First child of Root
Element = 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,"Donen");
      }
   }
}

产生输出:

Element = root
Value = Root
Value = 
Element = pai
Value = Pai_1
Value = 
Element = filho
Value = Pai1,Filho1

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

**编辑后更多编辑**

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读