前面寫了個perl的解析器,資訊打算存入到XML檔案中,
事實證明這可能是個錯誤的決定。因為當時我的想法是,第一版,可以在本機直接運行,不需要使用者裝資料庫。現在來看,真是不如用Access資料庫。
但既然選定這個方面,還是做下去。再回頭,時間更是來不及。
因為在調研解析器時,沒有找到好的.net 或是其它好用的編譯器,現在來看,製作編譯器還得C。
這樣一來,寫入XML的工具,最好也用C++來實現為好。
的確沒有時間來調研所有的XML庫,找到了Apach的Xerces C++,客觀來說,這個庫實現得相當不好用。
不過,與Apaph其它的項目類似,特別好編譯。編譯完成之後,立即對大部分例子,進行了跟蹤和分析,老實說,這些例子,相當不友好。
可能Xerces是由IBM主導的。拖遝冗長,而且說不到正題。因為這些樣本,都是為自動化的測試為目標的。
而我們程式員是使用的。
比如說,無非是幾個方面的可能使用:
1. 讀入和解析一個現有的XML檔案。
2. XSD語法檢查。
3. 製作記憶體XML串。
4. 寫入XML到磁碟檔案。
5. 讀入一個XML模板,添加資訊,存入檔案。
但,這些例子,完全是自說自話,沒有考慮到使用者的感受。
說到這裡,我是想勸那些未來使用XML的同仁,能不用Xerces就不要用。
這是我的調研結果。
但是,我還是不可能有時間,再重選一個。
找到了一些與Xerces相關的文章,絕大部分,都是來自於這個文章:
Xerces for C++ Using Visual C++, Part 2
注意,這個文章,原始應當是在IBM網站上。
part1就不要看了,一點用也沒有。只是講如保編譯和配置VC環境,如果你想到要用Xerces C++,相比,你沒有必要看。
因為我的目標,就是上面第5條:
5. 讀入一個XML模板,添加資訊,存入檔案。
結果,Xerces原始樣本裡竟然沒有——真的沒有,我找遍了。服了IBM。
然後,我就不得不仔細看這個文章,然後我發現,裡面最最關鍵的一個類:
DOMWriter 是沒有的。對了,我下的是3.3,真是越發讓我不舒服。
當然,不論你幹什麼,作為一個程式員,只要用了IBM參與的東西,你事先就得告誤訴自己,IBM,就是降低你的期望的意思,那裡一群學究,開發不出來什麼實用性好的東西。
不過,我個人而言,是非常痛恨這種無來由的不相容,而且最起碼的應用樣本中也沒有的情況。
然後,又找了很久,真的,至少有一下午,也沒有找到,雖然,我找到類似的類:DOMLSSerializer
也找到了,樣本中有利用這個類,把XML轉變成為流的代碼,但畢竟,我只是一個實現者,不想去把時間花在這上面,——寫完了還要測。
直到第二天早晨來,我又想了想,再回頭看這個文章:
Xerces for C++ Using Visual C++, Part 2 看回複,啊,回複裡面有:
DOMWriter replaced with DOMLSSerializer in new Xerces 3.0
原來,我想回退到2.8去,因為那個文章是用的2.8。
這也是今天本文的重點。
如果你開始學習XML Xerces C++
第一步,看 Xerces for C++ Using Visual C++, Part 2 第二步,下載Xerces 2.8,而不是3.0以上的版本。
3.0 不好用。
官方網站上有2.8 ,我從CSDN下了一個,完全沒有必要。
http://xerces.apache.org/xerces-c/applications.html
http://xerces.apache.org/xerces-c/install-2.html
還有一篇文章,
序列化 XML 資料
初步來說,我還是打算用2.8 。
如果用3.3 ,可以用如下程式碼完成:儲存到檔案:
This is a working version on Xerces 3.xenjoy ! //! \brief Save the DOM Document to the File System. void DoOutput2File(xercesc::DOMDocument* pDOMDocument, const wchar_t * FullFilePath ){DOMImplementation *pImplement = NULL;DOMLSSerializer *pSerializer = NULL; // @DOMWriterLocalFileFormatTarget *pTarget = NULL; //Return the first registered implementation that has the desired features. In this case, we are after//a DOM implementation that has the LS feature... or Load/Save. pImplement = DOMImplementationRegistry::getDOMImplementation(L"LS"); //From the DOMImplementation, create a DOMWriter.//DOMWriters are used to serialize a DOM tree [back] into an XML document. pSerializer = ((DOMImplementationLS*)pImplement)->createLSSerializer(); //@createDOMWriter(); //This line is optional. It just sets a feature of the Serializer to make the output//more human-readable by inserting line-feeds, without actually inserting any new elements/nodes//into the DOM tree. (There are many different features to set.) Comment it out and see the difference. // @pSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true); //DOMLSOutput *pOutput = ((DOMImplementationLS*)pImplement)->createLSOutput();DOMConfiguration *pConfiguration = pSerializer->getDomConfig(); // Have a nice outputif (pConfiguration->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))pConfiguration->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true); pTarget = new LocalFileFormatTarget(FullFilePath);pOutput->setByteStream(pTarget); // @pSerializer->write(pDOMDocument->getDocumentElement(), pOutput); // missing header "<xml ...>" if usedpSerializer->write(pDOMDocument, pOutput); delete pTarget;pOutput->release();pSerializer->release();}
不過,我發現一個好東東:
CodeSynthesis XSD
CodeSynthesis XSD is an open-source XML Schema to C++ data binding compiler that uses Xerces-C++ as the underlying XML parser. Provided with an XML instance specification (XML Schema), XSD generates C++ classes that represent the given vocabulary as well as parsing and serialization code. You can then access the data stored in XML using types and functions that semantically correspond to your application domain rather than dealing with direct representations of XML such as DOM and SAX.
XSD: XML Data Binding for C++
需要研究一下,看看所需要的Xerces版本是哪個。