XML Xerces c++学习笔记--(一)

前面写了个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 Pin

原来,我想回退到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.x
enjoy !
 

//! \brief Save the DOM Document to the File System.
 
void DoOutput2File(xercesc::DOMDocument* pDOMDocument, const wchar_t * FullFilePath )
{
DOMImplementation *pImplement = NULL;
DOMLSSerializer *pSerializer = NULL; // @DOMWriter
LocalFileFormatTarget *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 output
if (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 used
pSerializer->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版本是哪个。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值