2011-06-01 51 views
0

我想用Ruby和REXML编辑一个xml文件,但是在将文件写回到磁盘后,编码被改变了。但我需要保持文件的原始编码!如何防止REXML改变编码

这是我的XML编辑之前的样子:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'AdHoc|iPhone' "> 
    <DebugType>none</DebugType> 
    <Optimize>false</Optimize> 
    <OutputPath>..\Bin\iPhoneSimulator\AdHoc</OutputPath> 
    <WarningLevel>4</WarningLevel> 
    <MtouchUseSGen>false</MtouchUseSGen> 
    <MtouchDebug>False</MtouchDebug> 
    <CodesignKey>iPhone Developer:</CodesignKey> 
    <MtouchUseLlvm>false</MtouchUseLlvm> 
    <MtouchUseThumb>false</MtouchUseThumb> 
    <MtouchArch>ARMv6</MtouchArch> 
    <CodesignProvision>A2FBBCDB-218A-4CCC-88ED-A484AAE87EA5</CodesignProvision> 
    <MtouchI18n /> 
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks> 
    </PropertyGroup> 
    <ItemGroup> 
    <Reference Include="System" /> 
    <Reference Include="System.Xml" /> 
    <Reference Include="System.Core" /> 
    <Reference Include="monotouch" /> 
    <Reference Include="System.Xml.Linq" /> 
    <Reference Include="System.Web.Services" /> 
    <Reference Include="System.Json" /> 
    </ItemGroup> 

,并在这里写回磁盘后:

<PropertyGroup Condition=' &apos;$(Configuration)|$(Platform)&apos; == &apos;AdHoc|iPhone&apos; '> 
    <DebugType>none</DebugType> 
    <Optimize>false</Optimize> 
    <OutputPath>..\Bin\iPhoneSimulator\AdHoc</OutputPath> 
    <WarningLevel>4</WarningLevel> 
    <MtouchUseSGen>false</MtouchUseSGen> 
    <MtouchDebug>False</MtouchDebug> 
    <CodesignKey>iPhone Developer:</CodesignKey> 
    <MtouchUseLlvm>false</MtouchUseLlvm> 
    <MtouchUseThumb>false</MtouchUseThumb> 
    <MtouchArch>ARMv6</MtouchArch> 
    <CodesignProvision>A2FBBCDB-218A-4CCC-88ED-A484AAE87EA5</CodesignProvision> 
    <MtouchI18n/> 
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks> 
    </PropertyGroup> 
    <ItemGroup> 
    <Reference Include='System'/> 
    <Reference Include='System.Xml'/> 
    <Reference Include='System.Core'/> 
    <Reference Include='monotouch'/> 
    <Reference Include='System.Xml.Linq'/> 
    <Reference Include='System.Web.Services'/> 
    <Reference Include='System.Json'/> 
    </ItemGroup> 

这里是我的代码:

File.open('file.xml') do |config_file| 
# Open the document and edit the file 
config = Document.new(config_file) 
config.root.elements['PropertyGroup'].elements['CodesignKey'].text = 'my new developer' 

# Write the result to a new file. 
formatter = REXML::Formatters::Default.new 
File.open('file.xml', 'w') do |result| 
    formatter.write(config, result) 
end 
end 
+0

你知道哪个是原始编码?从文字看起来像html的字符编码... – Christian 2011-06-01 02:45:28

+0

TextWrangler说:Unicode(UTF-8,无BOM) – Matthias 2011-06-01 07:14:07

回答

0

这是与原始编码无关。你可以看到你的字符串被替换为html转义字符。 被替代为'''

相反REXML的,你可以尝试引入nokogiri宝石

一个基本的代码如下所示:

require 'rubygems' 
require 'nokogiri' 

filename = "file.xml" 
doc = Nokogiri::XML(File.new(filename)) 

# Check the node content 
doc.root.xpath("//MtouchArch").text 

# do your processing 
# ... 

doc.to_xml 
1

采取从this post。初始化Document对象后设置属性分隔符。

config = Document.new(config_file) 
config.context[:attribute_quote] = :quote