2011-06-01 178 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