NativeXml: A native Delphi XML parser and writer

http://www.simdesign.nl/xml.html

This software component contains a small-footprint Object Pascal (Delphi) XML implementation that allows to read and write XML documents. You basically only need one unit and you can simply add it to the "uses" clause.

You can use this software to read XML documents from files, streams or strings. The load routine generates events that can be used to display load progress on the fly. You can also use it to create and save XML documents.

Features

    • Read XML directly from stringstream or file with fast parser (1Mb/sec on 1.5GHz P3), writes to string, stream and file even faster.

    • Import of all common tags, including comments, CDATA, XML declarations and stylesheets.

    • Load from streaming media is supported; you can set up events to work with the node data while it comes in. See example 1. No need to keep the whole XML document in memory! Works well for huge documents. This is the SAX-like approach.

    • Save to file, string or stream in readablepreserve or compact format. Readable format will add linefeeds and indents to make the file readable in a text editor. Compact format sticks to the Xml specification and adds minimal control characters. Preserve format keeps the markup of the document exactly as it was.

    • Directly add blobs or streams in the XML file, they will be encoded as Binhex or Base64 (but you don't have to worry about that).

    • Many native Delphi types can be stored directly to the nodes, making this code very suitable to use while writing storage methods.
      Store and load boolean, single, double, word, integer, string, TFont, TColor, TPen, TBrush, TDateTime.
      You don't have to worry about escaping the strings; this is done automatically.

    • Loads and saves ANSI, UTF-8 as well as Unicode (UTF-16) documents and implements proper conversion warnings. Recognises streams even without byte order marks. NativeXml works internally with UTF8 encoded strings to preserve extended characters, and supports widestrings.

    • Unique feature: Store, read and create any TPersistent object to/from XML directly (see Example5). This is done by iterating through all of the objects' published properties by use of RTTI (runtime type information). This feature is only available for D5 and up.
    • New! Now you can use the binary format of NativeXml. This avoids the expensive parsing process to get the textual xml data into the document (DOM). And even better, the binary file size is much smaller, ~50% of file size with uncompressed binary files, and ~15% of the file size in zlib-compressed files. And you can use AES encryption to keep your sensitive files safe from prying eyes.
      The interface is the same whether textual xml or binary xml files are used, so you can work with binary files for speed and size purposes, then always send them to the public as normal xml files. 
  // TsdElementType enumerates the different kinds of elements that can be found
  // in the XML document.
  TsdElementType = (
    xeElement,     //  0 normal element <name {attr}> [value] [sub-elements] </name>

xeAttribute, // 1 attribute ( name='value' or name="value") : <element name='value'>....</element>
xeCharData,
// 2 character data in a node : <element>char data</element>
xeComment,
// 3 comment <!--{comment}-->
xeCData,
// 4 literal data <![CDATA[{data}]]>
xeCondSection,
// 5 conditional section <![ IGNORE / INCLUDE [ markup ]]>
xeDeclaration,
// 6 xml declaration <?xml{declaration}?>
xeStylesheet,
// 7 stylesheet <?xml-stylesheet{stylesheet}?>
xeDocType,
// 8 doctype dtd declaration <!DOCTYPE{spec}>
xeDtdElement,
// 9 dtd element <!ELEMENT >
xeDtdAttList,
// 10 dtd attlist <!ATTLIST >
xeDtdEntity,
// 11 dtd entity <!ENTITY >
xeDtdNotation,
// 12 dtd notation <!NOTATION >
xeInstruction,
// 13 processing instruction <?...?>
xeWhiteSpace,
// 14 chardata with only whitespace
xeQuotedText,
// 15 quoted text: "bla" or 'bla'
xeEndTag,
// 16 </...> and signal function in binary xml
xeError
// 17 some error or unknown ); TsdElementTypes = set of TsdElementType;

 

  // End-Of-Line style
  TsdEolStyle = (
    esLF,        // write End-Of-Line as just LF (#$0A) like normalised xml
    esCRLF,      // write End-Of-Line as CR + LF (#$0D + #$0A), for the windows platform
    esCR         // write End-Of-Line as CR (#$0D), for the mac platform
  );
  // Note on TNativeXml.XmlFormat:
  // - xfCompact (default) to save the xml fully compliant and at smallest size
  // - xfReadable writes additional nonsignificant whitespace so the client can easily read the xml file with a standard editor.
  // - xfPreserve aims to preserve whitespace data just as it is parsed
  TXmlFormatType = (
    xfCompact,  // Save without any control chars except LF after declaration
    xfReadable, // Save in readable format with indents and end-of-lines
    xfPreserve  // Preserve whitespace whenever possible
  );

 

{
  NativeXmlNodes:
  Parsing and writing methods for XML nodes:
  Attribute, CDATA, CharData, Comment, Declaration, DocType, DTD-AttList, DTD-Element, DTD-Entity, DTD-Notation, Element, (processing) Instruction, QuotedText, StyleSheet and ancestors:

  TXmlNode
    TsdContainerNode
      TsdElement
        TsdDtdElement
          TsdDtdAttList
          TsdDtdEntity
          TsdDtdNotation
      TsdDocType
      TsdDeclaration
        TsdStyleSheet
    TsdCharData
      TsdQuotedText
      TsdProcessingInstruction
      TsdComment
        TsdCData
        TsdConditionalSection
    TsdAttribute
}

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/shangdawei/p/3351543.html

强大的XML组件,可以将对象直接存入XML文件,非常实用,而且开源! 很不错,推荐大家使用! 原来一直使用Delphi自带的TXMLDocument类来进行XML文件的读写,虽然麻烦了一点,但功能上来说还不错。只是有一个地方让我实在不舒服 - 每次创建TXMLDocument类实例的时候必须传入TComponent对象,这就意味着,如果我把对XML文件的读写封装到一个类里面,那么在创建这个自定义类的时候就必须也传入TComponent对象。 我尝试过很多方法,但是都无法避免,最后试着上网找了找,于是就找到了NativeXML这个库。 下载之后马上打开Demo看了看,cool,创建TNativeXML的时候只需要传入xml文件路径就可以,再往下看就让我开始惊喜了。它已经把大多数操作都封装好了,而且还具有把任意对象序列化的能力。 比如,你能把整个Form通过 TsdXmlObjectWriter = class(TPersistent) 类存入一个XML文件,下次再读取。这样使得远程传输对象变得很简单了。 下面就是一个使用NativeXML库的例子: 目标XML结构: 2000-01-01 delphi 代码: procedure WriteTest; var xml : TNativeXML; n_bs : TXMLNode; begin //建立根节点 xml := TNativeXml.CreateName('bookshift'); xml.EncodingString := 'GB2312'; //输出风格为易读 xml.XmlFormat := xfReadable; //建立Book节点 n_bs := xml.Root.NodeNew('book'); //写入Book节点的属性 n_bs.WriteAttribuiteString('author','test_author'); //建立Book节点下属date节点并写入值 n_bs.WriteString('date','2000-01-01'); xml.SaveToFile('test.xml'); end; 打开test.xml文件看看吧,和上面的格式一摸一样。 很简单吧,我用这个库重写了原来的XML访问类,大约节省了40%的代码量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值