输出格式化的xml文件

作者: 喵帕斯 | 来源:发表于2016-08-04 10:15 被阅读946次

    输出格式化的xml文件

    上一篇我按照网上的资源写了一个创建xml文件的小程序,算让创建成功,但是在新增节点时发现格式不对,没有成xml的树状而是一行,详见我在简书的 创建xml文件系统 http://www.jianshu.com/p/36cc32096ac9
    经过分析和查阅资料简单的来说是xmlParseFilexmlReadFile的问题,两个函数都可以实现,只是实现方法有些不同。下面简单的说下两者的区别

    xmlParseFile

    我之前使用的就是xmlParseFile,它的功能是


    Function: xmlParseFile
    xmlDocPtr   xmlParseFile        (const char * filename)
    parse an XML file and build a tree. Automatic support for ZLIB/Compress compressed document is provided by default if found at compile-time。
    
    filename:   the filename
    Returns:    the resulting document tree if the file was wellformed, NULL otherwise.
    
    

    xmlParseFile是按默认的方式加载xml文件。需要注意的是如果要使用xmlParseFile加载文件,并修改保存话需要

    xmlKeepBlanksDefault(0) ;
    xmlIndentTreeOutput = 1 ;
    

    xmlKeepBlanksDefault

    Set and return the previous value for default blanks text nodes support. The 1.x version of the parser used an heuristic to try to detect ignorable white spaces. As a result the SAX callback was generating xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when using the DOM output text nodes containing those blanks were not generated. The 2.x and later version will switch to the XML standard way and ignorableWhitespace() are only generated when running the parser in validating mode and when the current element doesn't allow CDATA or mixed content. This function is provided as a way to force the standard behavior on 1.X libs and to switch back to the old mode for compatibility when running 1.X client code on 2.X . Upgrade of 1.X code should be done by using xmlIsBlankNode() commodity function to detect the "empty" nodes generated. This value also affect autogeneration of indentation when saving code if blanks sections are kept, indentation is not generated.
    val: int 0 or 1
    Returns: the last value for 0 for no substitution, 1 for substitution.
    --http://xmlsoft.org/

    根据上面的话当2.x版本时要设成1.

    xmlIndentTreeOutput

    Note that format = 1 provide node indenting only if xmlIndentTreeOutput = 1

    xmlReadFile

    xmlReadFile不仅可以加载文件还可以加载URL。


    Function: xmlReadFile
    xmlDocPtr   xmlReadFile(const char * filename,  const char * encoding, int options)
    parse an XML file from the filesystem or the network.
    
    filename:   a file or URL
    encoding:   the document encoding, or NULL
    options:    a combination of xmlParserOption
    Returns:    the resulting document tree
    

    使用xmlReadFile就不用做其他的处理,例如这次的小程序直接修改

    //doc = xmlParseFile(osd_block_file);
    doc = xmlReadFile(osd_block_file, NULL, XML_PARSE_NOBLANKS);
    

    就可以格式化输出了。

    运行结果


    上面的两种方法都可以实现格式化输出的功能,输出的xml文件如下

    <?xml version="1.0" encoding="UTF-8"?>
    <osd_block>
      <osd num="1">
        <str>初始化中</str>
        <size>4</size>
        <color>1</color>
        <x>50</x>
        <y>50</y>
      </osd>
      <osd num="1">
        <str>初始化中</str>
        <size>4</size>
        <color>1</color>
        <x>50</x>
        <y>50</y>
      </osd>
    </osd_block>
    

    比较

    虽然xmlParseFile和xmlReadFile都可以使用,但是建议还是要是用xmlReadFile
    在这[LINK]可以看下libxml2的作者是如何说的

    "Use xmlReadFile, it's the modern API for parsing"

    而且
    xmlReadFile比xmlParseFile更强大,xmlReadFile第三个参数提供了强大的选项

    Enum xmlParserOption { XML_PARSE_RECOVER = 1 : recover on errors 
    XML_PARSE_NOENT = 2 : substitute entities 
    XML_PARSE_DTDLOAD = 4 : load the external subset 
    XML_PARSE_DTDATTR = 8 : default DTD attributes 
    XML_PARSE_DTDVALID = 16 : validate with the DTD 
    XML_PARSE_NOERROR = 32 : suppress error reports 
    XML_PARSE_NOWARNING = 64 : suppress warning reports 
    XML_PARSE_PEDANTIC = 128 : pedantic error reporting 
    XML_PARSE_NOBLANKS = 256 : remove blank nodes 
    XML_PARSE_SAX1 = 512 : use the SAX1 interface internally 
    XML_PARSE_XINCLUDE = 1024 : Implement XInclude substitition 
    XML_PARSE_NONET = 2048 : Forbid network access 
    XML_PARSE_NODICT = 4096 : Do not reuse the context dictionary 
    XML_PARSE_NSCLEAN = 8192 : remove redundant namespaces declarations 
    XML_PARSE_NOCDATA = 16384 : merge CDATA as text nodes 
    XML_PARSE_NOXINCNODE = 32768 : do not generate 
    XINCLUDE START/END nodes 
    XML_PARSE_COMPACT = 65536 : compact small text nodes; no modification of the tree allowed afterwards (will possibly crash if you try to modify the tree) 
    XML_PARSE_OLD10 = 131072 : parse using
    XML-1.0 before update 5 
    XML_PARSE_NOBASEFIX = 262144 : do not fixup XINCLUDE xml:base uris 
    XML_PARSE_HUGE = 524288 : relax any hardcoded limit from the parser 
    XML_PARSE_OLDSAX = 1048576 : parse using SAX2 interface before 2.7.0 
    XML_PARSE_IGNORE_ENC = 2097152 : ignore internal document encoding hint XML_PARSE_BIG_LINES = 4194304 : Store big lines numbers in text PSVI field}
    

    下一篇将尝试解析和修改。

    相关文章

      网友评论

        本文标题:输出格式化的xml文件

        本文链接:https://www.haomeiwen.com/subject/hcaqsttx.html