如何防止 REXML 更改编码

发布于 2024-11-11 07:03:42 字数 2964 浏览 3 评论 0原文

我正在尝试使用 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=' &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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云归处 2024-11-18 07:03:42

摘自这篇文章。初始化Document对象后设置属性分隔符。

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

Taken from this post. Set the attribute delimiter character after initializing Document object.

config = Document.new(config_file)
config.context[:attribute_quote] = :quote
趁微风不噪 2024-11-18 07:03:42

它与原始编码并没有真正的关系。您可以看到您的字符串已被替换为 html 转义字符。 " 替换为 '' 替换为 '

而不是 REXML,您可以尝试使用 Nokogiri gem

基本代码看起来像这样:

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

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:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文