輸出格式化的xml文件

輸出格式化的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)單的來說是xmlParseFilexmlReadFile的問題,兩個(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}

下一篇將嘗試解析和修改。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評(píng)論 19 139
  • 我該如何用一行去描述未完的遺憾? 如此,這般?
    化濁閱讀 320評(píng)論 0 0
  • 阿厘分享,缺乏行動(dòng)力的天賦才華落地,需要認(rèn)可 信念沒有用: 剛在畢業(yè)群,分享我馬上要去落地天賦去別的地方做活動(dòng),我...
    Ali阿厘閱讀 488評(píng)論 0 1
  • 寫完這個(gè)標(biāo)題發(fā)覺太大了點(diǎn),現(xiàn)在開個(gè)公眾號(hào),不叫自媒體,叫內(nèi)容創(chuàng)業(yè)。概念更迭太快,離開北上廣的人有點(diǎn)快跟不上。 這一...
    楊夏閱讀 346評(píng)論 0 3
  • 這幾年斷斷續(xù)續(xù)練過幾年瑜伽,今年重新開始跟著老師訓(xùn)練,發(fā)現(xiàn)練了一段時(shí)間沒什么進(jìn)步,往后跳重得像秤砣,也沒找到所謂腹...
    落落的蘿卜糕閱讀 544評(píng)論 0 1

友情鏈接更多精彩內(nèi)容