解析xml文件Delphi [英] Parsing xml file Delphi

查看:145
本文介绍了解析xml文件Delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是新的编码和Delphi,已经开始和关闭了几个月。



以下是一个示例xml文件。



我想要实现的是解析.xml文件的每个名称部分中的所有数据。


$ b $我从来没有这样做过。一些指导将不胜感激。我已经看过这里的其他问题类似于这一点,但我不能很好地掌握它。



我没有代码示例提供....这是怎么回事卡住了我,我不知道从哪里开始。

 < ds> 
< Customers>
< Name>
< address_name> test 1< / address_name>
< address_line_1>测试1< / address_line_1>
< address_line_2>测试1< / address_line_2>
< address_line_3>测试1< / address_line_3>
< postcode> test 1< / postcode>
< tel_no> test 1< / tel_no>
< fax_no> test 1< / fax_no>
< email_address> test 1< email_address />
< website> test 1< website />
< / Name>
< Name>
< address_name> test 2< / address_name>
< address_line_1> test 2< / address_line_1>
< address_line_2> test 2< / address_line_2>
< address_line_3> test 2< / address_line_3>
< postcode> test 2< / postcode>
< tel_no> test 2< / tel_no>
< fax_no> test 2< / fax_no>
< email_address> test 2< email_address />
< website> test 2< website />
< / Name>
< Name>
< address_name> test 3< / address_name>
< address_line_1> test 3< / address_line_1>
< address_line_2> test 3< / address_line_2>
< address_line_3> test 3< / address_line_3>
< postcode> test 3< / postcode>
< tel_no> test 3< / tel_no>
< fax_no> test 3< / fax_no>
< email_address>测试3< email_address />
< website> test 3< website />
< / Name>
< Customers>
< / ds>

谢谢,

解决方案

根据您的Delphi SKU,如果带有XMLMapper实用程序(在Delphi的Bin目录中),可以非常方便地使用Delphi组件。



创建一个包含




  • 一个TClientDataSet

  • 一个TDatasource

  • 的新项目将数据源连接到CDS和网格到数据源;


    $ p
    $ / b $ b

    • 添加一个XMLTransformerProvider



    将CDS的ProviderName设置为XMLTransformerProvider



    在FormCreate事件中打开CDS。



    保存项目。



    然后,在我的评论中提到的更正您的XML文件后,
    将其加载到Delphi的XMLMapper中。



    在XML Mapper,




    • 选择LH,Documen的 DocumentView t窗格


    • 依次双击每个节点address_name ..网站


    • 点击创建|来自XML的DataPacket


    • 点击中央的映射选项卡上的创建和测试转换转换窗格。


    • 从菜单中,转到文件|保存|转换并保存.Xtr文件。




    回到Delphi项目中,指出XMLDataFile属性XMLTransformProvider在您的XML文件&
    您的.Xtr文件中的TransformRead zub组件的TransformationFile。



    编译&运行您的项目。



    您的CDS中创建的TField具有由XMLMapper生成的.Xtr文件确定的类型和大小。您可以按如下方式微调这些:



    在XMLMapper中:




    • 选择转换窗格中的节点属性选项卡。


    • 文档文档窗格中的查看标签,点击其中一个数据节点 -


    • 然后您可以设置其



    Firstly, I'm new to coding and Delphi, been on and off with it for a few months.

    Below is a sample xml file.

    What I'm trying to achieve is to Parse all data in each 'Name' section of the .xml file.

    I have never done this before. Some guidance would be appreciated. I have looked at other questions on here similar to this but I cant quite get to grips with it.

    I have no code example to provide....this is how stuck I am, I do not know where to begin.

    <ds>
    <Customers>
    <Name>
    <address_name>test 1</address_name> 
    <address_line_1>test 1</address_line_1> 
    <address_line_2>test 1</address_line_2> 
    <address_line_3>test 1</address_line_3>
    <postcode>test 1</postcode> 
    <tel_no>test 1</tel_no> 
    <fax_no>test 1</fax_no> 
    <email_address>test 1<email_address/> 
    <website>test 1<website /> 
    </Name>
    <Name>
    <address_name>test 2</address_name> 
    <address_line_1>test 2</address_line_1> 
    <address_line_2>test 2</address_line_2> 
    <address_line_3>test 2</address_line_3>
    <postcode>test 2</postcode> 
    <tel_no>test 2</tel_no> 
    <fax_no>test 2</fax_no> 
    <email_address>test 2<email_address/> 
    <website>test 2<website /> 
    </Name>
    <Name>
    <address_name>test 3</address_name> 
    <address_line_1>test 3</address_line_1> 
    <address_line_2>test 3</address_line_2> 
    <address_line_3>test 3</address_line_3>
    <postcode>test 3</postcode> 
    <tel_no>test 3</tel_no> 
    <fax_no>test 3</fax_no> 
    <email_address>test 3<email_address/> 
    <website>test 3<website /> 
    </Name>
    <Customers>
    </ds>
    

    Thanks,

    解决方案

    Depending on your Delphi SKU, you can do this using Delphi components very easily if it comes with the XMLMapper utility (in Delphi's Bin directory).

    Create a new project containing

    • a TClientDataSet
    • a TDatasource
    • a TDbGrid

    Connect the datasource to the CDS and the grid to the datasource;

    • add an XMLTransformerProvider

    Set the ProviderName of the CDS to the name of the XMLTransformerProvider

    In the FormCreate event, open the CDS.

    Save the project.

    Then, after correcting your XML file as I mentioned in my comment, load it into Delphi's XMLMapper.

    In XML Mapper,

    • Select the DocumentView tab of the LH, Document pane

    • Double-click each of the nodes address_name .. website in turn

    • Click Create | DataPacket from XML in the menu

    • Click the Create and Test Transformation button on the Mapping tab of the central, Transformation pane.

    • From the menu, go to File | Save | Transformation and save your .Xtr file.

    Back in your Delphi project, point the XMLDataFile property of the XMLTransformProvider at your XML file, & the TransformationFile of its TransformRead zub-component at your .Xtr file.

    Compile & run your project.

    The TFields created in your CDS have types and sizes determined by the .Xtr file generated by XMLMapper. You can fine-tune these as follows:

    In XMLMapper:

    • Select the Node Properties tab of the Transformation pane.

    • In the Document View tab of the Document pane, click one of the data nodes-

    • You can then set its Data Type and Max Length on the Node Propertyies tab.

    这篇关于解析xml文件Delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆