perl中显示XML结构

简介: $ cat -n simple1.pl 显示XML的代码 1    #!/usr/bin/perl2    # simple.pl3    use Data::Dumper;4    use warnings;5    use strict;6    use XML::Simple;7    my...

$ cat -n simple1.pl 显示XML的代码

1    #!/usr/bin/perl
2    # simple.pl
3    use Data::Dumper;
4    use warnings;
5    use strict;
6    use XML::Simple;
7    my ($simple,$data,$date);
8    $simple=new XML::Simple(forcearray=>1);
9    $data=$simple->XMLin('./invoice.xml');
10   
11    print Dumper $data;

其中invoice.xml为

<?xml version="1.0" standalone="no"?>
<!DOCTYPE invoice SYSTEM "inXML.dtd">

<invoice partner="Lendl's Widgets" partner_id="0000LW">
    <number>L593</number>
    <customer>
        <status>new</status>
        <name>Mike Seaver</name>
        <email>mikeseaver@mycompany.com</email>
        <address>
            <street>438 Warm Springs Boulevard</street>
            <location>Suite 127</location>
            <city>San Francisco</city>
            <state>CA</state>
            <zip>95412</zip>
            <notes>Across from the clock tower.</notes>
        </address>
    </customer>
    <date>
        <month>10</month>
        <day>27</day>
        <year>2000</year>
    </date>
    <items>
        <item>
            <part_id>R96737</part_id>
            <quantity>1</quantity>
            <description>Extra Long Titanium and Graphite Racquet</description>
            <price currency="dollars">94.99</price>
        </item>
        <item>
            <part_id>R9263</part_id>
            <quantity>1</quantity>
            <description>Super Support Cross Trainers</description>
            <price currency="dollars">57.99</price>
        </item>
    </items>
    <billing>
        <subtotal currency="dollars">152.98</subtotal>
        <tax rate="8.25" unit="percent" currency="dollars">12.62</tax>
        <total currency="dollars">165.60</total>
        <payment type="cc">
            <reference id="VISA" expiration="06/01">
                1234 5678 9012 3456
            </reference>
            <amount currency="dollars">165.60</amount>
        </payment>
    </billing>
</invoice>

显示后的结构为:

$VAR1 = {
          'number' => [
                      'L593'
                    ],
          'date' => [
                    {
                      'month' => [
                                 '10'
                               ],
                      'day' => [
                               '27'
                             ],
                      'year' => [
                                '2000'
                              ]
                    }
                  ],
          'customer' => [
                        {
                          'email' => [
                                     'mikeseaver@mycompany.com'
                                   ],
                          'status' => [
                                      'new'
                                    ],
                          'name' => [
                                    'Mike Seaver'
                                  ],
                          'address' => [
                                       {
                                         'zip' => [
                                                  '95412'
                                                ],
                                         'city' => [
                                                   'San Francisco'
                                                 ],
                                         'location' => [
                                                       'Suite 127'
                                                     ],
                                         'notes' => [
                                                    'Across from the clock tower.'
                                                  ],
                                         'street' => [
                                                     '438 Warm Springs Boulevard'
                                                   ],
                                         'state' => [
                                                    'CA'
                                                  ]
                                       }
                                     ]
                        }
                      ],
          'partner_id' => '0000LW',
          'billing' => [
                       {
                         'payment' => [
                                      {
                                        'amount' => [
                                                    {
                                                      'currency' => 'dollars',
                                                      'content' => '165.60'
                                                    }
                                                  ],
                                        'reference' => {
                                                       'VISA' => {
                                                                 'expiration' => '06/01',
                                                                 'content' => '
                1234 5678 9012 3456
            '
                                                               }
                                                     },
                                        'type' => 'cc'
                                      }
                                    ],
                         'subtotal' => [
                                       {
                                         'currency' => 'dollars',
                                         'content' => '152.98'
                                       }
                                     ],
                         'tax' => [
                                  {
                                    'currency' => 'dollars',
                                    'rate' => '8.25',
                                    'unit' => 'percent',
                                    'content' => '12.62'
                                  }
                                ],
                         'total' => [
                                    {
                                      'currency' => 'dollars',
                                      'content' => '165.60'
                                    }
                                  ]
                       }
                     ],
          'items' => [
                     {
                       'item' => [
                                 {
                                   'part_id' => [
                                                'R96737'
                                              ],
                                   'quantity' => [
                                                 '1'
                                               ],
                                   'price' => [
                                              {
                                                'currency' => 'dollars',
                                                'content' => '94.99'
                                              }
                                            ],
                                   'description' => [
                                                    'Extra Long Titanium and Graphite Racquet'
                                                  ]
                                 },
                                 {
                                   'part_id' => [
                                                'R9263'
                                              ],
                                   'quantity' => [
                                                 '1'
                                               ],
                                   'price' => [
                                              {
                                                'currency' => 'dollars',
                                                'content' => '57.99'
                                              }
                                            ],
                                   'description' => [
                                                    'Super Support Cross Trainers'
                                                  ]
                                 }
                               ]
                     }
                   ],
          'partner' => 'Lendl\'s Widgets'
        };

相关文章
|
6月前
|
XML 存储 JavaScript
DOM 节点遍历:掌握遍历 XML文档结构和内容的技巧
**XML DOM 遍历、操作和导航概述** - 遍历XML节点树用于提取信息,例如,通过JavaScript的DOM API循环`&lt;book&gt;`子节点显示名称和值。 - DOM解析器处理XML文本数据,包括解析字符数据(PCDATA)和识别CDATA段。 - 节点导航涉及`parentNode`、`childNodes`等属性,`get_nextSibling`等辅助函数避免空文本节点。 - `getElementsByTagName`、`getAttribute`和`nodeValue`用于检索元素、属性值和文本。
89 6
DOM 节点遍历:掌握遍历 XML文档结构和内容的技巧
|
6月前
|
XML JavaScript 数据格式
XML Schema(XSD)详解:定义 XML 文档结构合法性的完整指南
XML Schema(XSD)是用于定义XML文档结构的规范,它描述了元素、属性及其顺序、数据类型和约束。与DTD相比,XML Schema支持更多数据类型,如字符串、日期等,并且是基于XML的,允许扩展和重用。学习XML Schema有助于确保数据通信时双方对内容的理解一致,通过验证防止错误。示例展示了如何定义一个`note`元素,包含`to`, `from`, `heading`和`body`子元素,都是字符串类型。XML文档可以通过引用XML Schema进行验证,确保内容符合预设模式。
472 0
|
XML JSON JavaScript
36jqGrid 数据结构 - XML结构
36jqGrid 数据结构 - XML结构
35 0
|
XML Java Android开发
#4,Android Studio Android程序结构 工程目录介绍 文件作用 运行配置文件AndroidManifest.xml
#4,Android Studio Android程序结构 工程目录介绍 文件作用 运行配置文件AndroidManifest.xml
|
XML 安全 数据格式
Arcgis工具箱无法使用,显示“XML包含错误“的解决方法
Arcgis工具箱无法使用,显示“XML包含错误“的解决方法
1103 0
Arcgis工具箱无法使用,显示“XML包含错误“的解决方法
|
XML JavaScript Java
Java Web之JSP操作XML(XML的文档结构 语法和注释、dom4j的下载与配置 应用dom4j创建、解析和修改XML)
Java Web之JSP操作XML(XML的文档结构 语法和注释、dom4j的下载与配置 应用dom4j创建、解析和修改XML)
263 0
Java Web之JSP操作XML(XML的文档结构 语法和注释、dom4j的下载与配置 应用dom4j创建、解析和修改XML)
|
XML 缓存 Android开发
Android Studio Dolphin | 2021.3.1不显示布局XML预览
Android Studio Dolphin | 2021.3.1不显示布局XML预览
2047 0
Android Studio Dolphin | 2021.3.1不显示布局XML预览
|
XML 数据格式
Mac环境IDEA编写XML行注释不显示在行首
Mac环境IDEA编写XML行注释不显示在行首
83 0
|
XML 计算机视觉 数据格式
目标检测脚本 | 根据xml文件在原图上显示边界框并保存
目标检测脚本 | 根据xml文件在原图上显示边界框并保存
196 0
|
XML 数据格式
Mac环境IDEA编写XML行注释不显示在行首
Mac环境IDEA编写XML行注释不显示在行首
121 0