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
4

2 に答える 2

1

この投稿から取得。Document オブジェクトを初期化した後、属性区切り文字を設定します。

config = Document.new(config_file)
config.context[:attribute_quote] = :quote
于 2012-09-26T08:36:09.430 に答える
0

元のエンコーディングとは実際には関係ありません。文字列が 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
于 2011-06-01T16:27:47.913 に答える