首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止REXML更改编码

如何防止REXML更改编码
EN

Stack Overflow用户
提问于 2011-06-01 10:23:12
回答 2查看 657关注 0票数 0

我正在尝试使用Ruby和REXML编辑xml文件,但是在将文件写回磁盘后,编码发生了变化。但是我需要保留文件的原始编码!

这是我的xml在编辑之前的样子:

代码语言:javascript
复制
  <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>

在将其写回磁盘后:

代码语言:javascript
复制
  <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>

下面是我的代码:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2012-09-26 16:36:09

摘自this post。初始化单据对象后设置属性分隔符。

代码语言:javascript
复制
config = Document.new(config_file)
config.context[:attribute_quote] = :quote
票数 1
EN

Stack Overflow用户

发布于 2011-06-02 00:27:48

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

您可以尝试使用Nokogiri gem来代替REXML

基本代码如下所示:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6195582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档