如何防止 REXML 更改编码
我正在尝试使用 Ruby 和 REXML 编辑 xml 文件,但将文件写回磁盘后,编码发生了更改。但我需要保留文件的原始编码!
这是我的 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=' '$(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>
这是我的代码:
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
I'm trying to edit a xml file with Ruby and REXML but after writing the file back to disk the encoding is changed. But i need to keep the original encoding of the file!
This is what my xml looks before editing:
<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>
and here after writing it back to disk:
<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>
Here is my code:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
摘自这篇文章。初始化Document对象后设置属性分隔符。
Taken from this post. Set the attribute delimiter character after initializing Document object.
它与原始编码并没有真正的关系。您可以看到您的字符串已被替换为 html 转义字符。 " 替换为 ',' 替换为 '
而不是 REXML,您可以尝试使用 Nokogiri gem
基本代码看起来像这样:
It is not really related with the original encoding. You can see that your strings were replaced with html escaping characters. " was replaced with ' and ' with '
Instead of REXML, you can try the Nokogiri gem
A basic code looks like this: