輸出格式化的xml文件
上一篇我按照網(wǎng)上的資源寫了一個(gè)創(chuàng)建xml文件的小程序,算讓創(chuàng)建成功,但是在新增節(jié)點(diǎn)時(shí)發(fā)現(xiàn)格式不對(duì),沒有成xml的樹狀而是一行,詳見我在簡(jiǎn)書的 創(chuàng)建xml文件系統(tǒng) http://www.itdecent.cn/p/36cc32096ac9。
經(jīng)過分析和查閱資料簡(jiǎn)單的來說是xmlParseFile和xmlReadFile的問題,兩個(gè)函數(shù)都可以實(shí)現(xiàn),只是實(shí)現(xiàn)方法有些不同。下面簡(jiǎn)單的說下兩者的區(qū)別
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是按默認(rèn)的方式加載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/
根據(jù)上面的話當(dāng)2.x版本時(shí)要設(shè)成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);
就可以格式化輸出了。
運(yùn)行結(jié)果
上面的兩種方法都可以實(shí)現(xiàn)格式化輸出的功能,輸出的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更強(qiáng)大,xmlReadFile第三個(gè)參數(shù)提供了強(qiáng)大的選項(xiàng)
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}
下一篇將嘗試解析和修改。