---
title: "使用XML::Simple来读写XML文件"
author: "standalone"
date: "2012-12-18T17:38:31Z"
tags: []
categories: ["编程语言"]
slug: "xml-simple-xml"
draft: false
lastmod: "2012-12-18T17:38:31Z"
id: 1749885
url: "https://standalone.iteye.com/blog/1749885"
---

有时对于一个XML配置文件需要简单的将其load到内存生成perl的data structure，然后做了相应改动之后再写到文件中去， 用XML::Simple就比较方便。

但是在使用的过程中发现如果不小心很容易将原文件的结构跟最后dump出去的文件的结构变得不一致，此时要注意使用options。

以下使用example：

my $xml = XML::Simple->new();
my $data = $xml->XMLin($file, ForceArray => 1);

# some changes to $data


open my $fh, '>', $file or die $!;
$xml->XMLout($data, OutputFile => $fh, RootName => 'root');