相关文章推荐
勤奋的柿子  ·  Jackson XML ...·  1 周前    · 
留胡子的西瓜  ·  jacksonxmlproperty - ...·  5 天前    · 
大气的凳子  ·  使用Jackson ...·  5 天前    · 
逆袭的红酒  ·  sql查询数量大于1-掘金·  1 年前    · 
安静的弓箭  ·  Getting Started With ...·  1 年前    · 

我不关心文件是否是 "适当的XML "或所有这些,我不关心DTD或其他标准XML的要求。我只需要读取和解析这些值。

有没有人知道除了lib eXpat以外还有什么库可以做到这一点,或者有什么代码可以做到这一点?

1 个评论
当要求提供库时,你可能希望提到你的平台,因为库可能是特定平台的。
c
xml
Jessica
Jessica
发布于 2010-05-18
6 个回答
WhirlWind
WhirlWind
发布于 2022-01-16
已采纳
0 人赞同
谢谢,我已经检查过了,1)我花了很长时间才明白他们要我做什么(为了让它工作),2)它抱怨说我没有DTD,我的文件没有正确的格式。
我想的是更多的东西,大致如下 codeguru.com/cpp/data/data-misc/xml/article.php/c4549
It shouldn't require a DTD, but it does require that the XML is well-formed -- i.e. that the start/end tags match up and that there are no illegal characters and that there is one and only root element. If your input is not well formed, maybe you should fix it. If you don't require your input to be well formed XML, but just something xml-ish, then you can easily write your own parser: Just search for the "<" & ">" chars to break it into pieces and then parse each piece. A lot of the complexity of an XML parser is because it has to parse any generalized XML into a specific internal model.
不要。永远不要尝试写你自己的XML解析器,永远不要尝试用字符串分割器来解析XML。你会有编码和转义问题。你将会有互操作性的问题。你将会有安全问题。为你的平台使用一个经过验证、测试和建立的库,例如expat或libxml2,就像这里的许多其他评论建议的那样。
anon
发布于 2022-01-16
0 人赞同

The 外籍人士 解析器是我遇到过的最好的解析器--我在我的C++代码中使用它,而不是各种C++解析器--但它是用C语言编写的,非常容易使用和嵌入你的应用程序。所以我不明白为什么你的问题中说。

(除lib eXpat外)

你有什么反对意见吗?

的确,考虑到给定的要求,我肯定会选择expat。我能想到的在这种情况下不使用它的唯一原因是,如果发帖人没有掌握从SAX事件中创建数据结构,从而需要一个DOM?
不,我不反对它,事实上我正在另一个项目上使用它,但是它很大,而且对于我需要的东西来说,它比我需要的东西更复杂。因此,正如我在问题中明确指出的,我不会用expat作为解决方案
anon
@Jessica 你一定对 "大 "字有你自己的定义--Expat是你能得到的最小的XML解析器。具体来说,它比Mini-XML还小。
我在哪里可以了解到在C语言平台上使用expat进行xml解析?
code4life
code4life
发布于 2022-01-16
0 人赞同

Mini-XML怎么样? 它是轻量级的,与gcc一起工作,与ANSI-C兼容...

http://www.minixml.org/index.php

根据文档,要搜索特定的节点,可以像这样简单。

/* Find the first "a" element */
    node = mxmlFindElement(tree, tree, "a",
                           NULL, NULL,
                           MXML_DESCEND);

一旦你得到了这个节点,你就可以根据你的要求来操纵它。

谢谢,我已经发了这个帖子(见我自己对问题的回答)。
Len Holgate
Len Holgate
发布于 2022-01-16
0 人赞同

If C++ is OK then you might try TinyXML .我已经用了好几年了,效果很好。

Jessica
Jessica
发布于 2022-01-16
0 人赞同

Mini-XML looks promising

http://www.minixml.org/

Nandkishor Biradar
Nandkishor Biradar
发布于 2022-01-16
0 人赞同

你可以考虑 迷你ML-Parser ,是一个简单而微小的C语言的XML解析器库。

  • Unlike other XML parser it is extremely easy to use. You need to call only one API to parse your XML data.
  • It is a validating parser. It validates the syntax of XML data and also extracts the content of XML data.
  • 为了从XML数据中解析和提取内容,你需要向解析器提供一些信息。例如,XML元素名称、其子元素、属性、内容类型等。解析器使用 xs_element_t 结构来保存XML数据的所有这些属性

    在你的例子中,你有两个XML元素 "user "和 "参数"。 user 是一个根元素,并且 参数 是一个子元素。属性 "name "和 "value "保存内容。为了提取这些内容,你需要指定内容类型和目标地址来存储这些内容。

    例子 xs_element_t 为你的XML数据。

    const xs_element_t user_root =
        .Name.String = "user",                 //!< name of XML element
        .Name.Length = 4,                      //!< Length of name
        .Attribute_Quantity = 1,               //!< Number of attributes of this element
        .Attribute          = attributes,      //!< Address of structure containing attributes
        .Child_Quantity = 1,                   //!< Number of child elements
        .Child          = &param_element,      //!< Address of structure holding child elements
    const xs_element_t param_element =
        .Name.String = "param",                //!< name of XML element
        .Name.Length = 5,                      //!< Length of name
        .Attribute_Quantity = 2,               //!< Number of attributes of this element
        .Attribute          = attributes,      //!< Address of structure containing attributes    
    //! Holds properties of attributes
    const xs_attribute_t attributes[] =
        [0].Name.String = "name",           //!< Name of XML attribute
        [0].Name.Length = 4,                //!< Length of attribute name
        [0].Target.Type = EN_STATIC,        //!< Target address type is static.
        [0].Target.Address = &name,         //!< Target address offset from the parent target address
        [0].Content.Type   = EN_STRING,     //!< Content type is string.             
        [1].Name.String = "value",          //!< Name of XML attribute
        [1].Name.Length = 4,                //!< Length of attribute name
        [1].Target.Type = EN_STATIC,        //!< Target address type is static.
        [1].Target.Address = &value,        //!< Target address offset from the parent target address
        [1].Content.Type   = EN_STRING,     //!< Content type is string.             
    

    注意:上述结构并不完整。为了使例子简单,我省略了一些结构成员。

    要解析xml数据,请调用parse_xml()并传递地址为user_root(根元素)和XML数据。

    parse_xml(&user_root, xml_data, NULL);