尽管可以使用单引号或双引号定义XML属性,但我的用户正在尝试将我的软件与另一个不接受单引号属性值的软件集成.
我用户REXML来生成我的XML.
有没有办法让REXML生成双引号属性值?如果没有,有没有办法让我轻松转换它?
谢谢
在今天升级到Ruby-1.9.3-p392之后,REXML在尝试检索特定大小的XML响应时抛出运行时错误 - 一切正常并且在接收25个XML记录时没有引发错误,但是一旦某个XML响应达到长度阈值,我得到这个错误:
Error occurred while parsing request parameters.
Contents:
RuntimeError (entity expansion has grown too large):
/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/rexml/text.rb:387:in `block in unnormalize'
Run Code Online (Sandbox Code Playgroud)
我意识到最新的Ruby版本已经改变了:http: //www.ruby-lang.org/en/news/2013/02/22/rexml-dos-2013-02-22/
作为一个快速修复,我已经将大小更改REXML::Document.entity_expansion_text_limit
为更大的数字,错误就消失了.
是否存在风险较低的解决方案?
我正在使用REXML构建一个xml文档,并希望以特定方式输出到文本.doc是一个CuePoint标签列表,我用Element.new和add_element生成的那些标签都拼凑成一行如下:( stackoverflow在这里将它们分成两行,但想象下面的全部是一条线):
<CuePoint><Time>15359</Time><Type>event</Type><Name>inst_50</Name></CuePoint><CuePoint><Time>16359</Time><Type>event</Type><Name>inst_50</Name></CuePoint>
当我将它们保存到文件时,我希望它们看起来像这样:
<CuePoint>
<Time>15359</Time>
<Type>event</Type>
<Name>inst_50</Name>
</CuePoint>
<CuePoint>
<Time>16359</Time>
<Type>event</Type>
<Name>inst_50</Name>
</CuePoint>
Run Code Online (Sandbox Code Playgroud)
我尝试将.write函数传递给值2,以缩进它们:这会产生以下结果:
xml.write($stdout, 2)
产生
<CuePoint>
<Time>
15359
</Time>
<Type>
event
</Type>
<Name>
inst_50
</Name>
</CuePoint>
<CuePoint>
<Time>
16359
</Time>
<Type>
event
</Type>
<Name>
inst_50
</Name>
</CuePoint>
Run Code Online (Sandbox Code Playgroud)
这是不需要的,因为它已经将空白插入到只有文本的标签内容中.即Name标签的内容现在是"\n inst_50 \n"或其他东西.这将会破坏读取xml的应用程序.
有谁知道我怎么能按照我想要的方式格式化输出文件?
感谢任何建议,最多
编辑 - 我刚刚通过另一个StackOverflow帖子在ruby-forum上找到答案:http://www.ruby-forum.com/topic/195353
formatter = REXML::Formatters::Pretty.new
formatter.compact = true
File.open(@xml_file,"w"){|file| file.puts formatter.write(xml.root,"")}
Run Code Online (Sandbox Code Playgroud)
这会产生类似的结果
<CuePoint>
<Time>33997</Time>
<Type>event</Type>
<Name>inst_45_off</Name>
</CuePoint>
<CuePoint>
<Time>34080</Time>
<Type>event</Type>
<Name>inst_45</Name>
</CuePoint>
Run Code Online (Sandbox Code Playgroud)
CuePoint标签之间没有额外的界限,但这对我来说很好.我将这个问题留在这里以防万一其他人偶然发现它.
我不知道这是什么名字,这使我的搜索变得复杂.
我的数据文件OX.session.xml是(旧的?)形式
<?xml version="1.0" encoding="utf-8"?>
<CAppLogin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://oxbranch.optionsxpress.com">
<SessionID>FE5E27A056944FBFBEF047F2B99E0BF6</SessionID>
<AccountNum>8228-5500</AccountNum>
<AccountID>967454</AccountID>
</CAppLogin>
Run Code Online (Sandbox Code Playgroud)
那个XML数据格式究竟叫什么?
无论如何,我想要的是在我的Ruby代码中最终得到一个哈希,如下所示:
CAppLogin = { :SessionID => "FE5E27A056944FBFBEF047F2B99E0BF6", :AccountNum => "8228-5500", etc. } # Doesn't have to be called CAppLogin as in the file, may be fixed
Run Code Online (Sandbox Code Playgroud)
什么可能是最短的,最内置的Ruby方式来自动化哈希读取,我可以更新SessionID值并将其轻松存储回文件以供以后的程序运行?
我玩过YAML,REXML但是还不想打印我的(坏)示例试验.
我正在创建一个XML文档:我想至少进行单元测试,以确保它的格式正确.到目前为止,我只能通过使用REXML库中的'hasElements'来估算它.
有没有更好的办法 ?最好使用内置库(我的意思是随标准Ruby 1.8.x发行版一起提供的库).
require "test/unit"
require 'rexml/document'
require 'test/unit/ui/console/testrunner'
include REXML
class TestBasic < Test::Unit::TestCase
def test_createXML
my_xml=...create doc here...
doc = Document.new(my_xml);
assert(doc.has_elements?);
end
end
Test::Unit::UI::Console::TestRunner.run(TestBasic);
Run Code Online (Sandbox Code Playgroud) 我有一个XML,可以是
<?xml version="1.0" encoding="utf-8"?>
<testnode type="1">123</testnode>
Run Code Online (Sandbox Code Playgroud)
或者喜欢
<?xml version="1.0" encoding="utf-8"?>
<othernode attrib="true">other value</othernode>
Run Code Online (Sandbox Code Playgroud)
或者根节点可能是完全出乎意料的.(理论上任何东西.)我正在使用REXML来解析它.如何找出哪个XML节点是根元素?
我有一个非常简单的xml文件,我试图访问:
<article>
<text>hello world</text>
</article>
Run Code Online (Sandbox Code Playgroud)
我到目前为止这样做:
file = File.open("#{Rails.root}/public/files/#{file_id}.xml", "r")
xml = file.read
doc = REXML::Document.new(xml)
Run Code Online (Sandbox Code Playgroud)
当我在rails控制台中运行此代码时,我看到:
1.9.3-p194 :033 > doc.inspect
=> "<UNDEFINED> ... </>"
Run Code Online (Sandbox Code Playgroud)
我似乎无法理解为什么它没有正确加载文件,我也无法访问文本xml元素.
我正在使用REXML Ruby解析器来解析XML文件.但是在64位带有64位Ruby的AIX盒子上,我收到以下错误:
REXML::ParseException: #<REXML::ParseException: #<RegexpError: Stack overflow in
regexp matcher:
/^<((?>(?:[\w:][\-\w\d.]*:)?[\w:][\-\w\d.]*))\s*((?>\s+(?:[\w:][\-\w\d.]*:)?[\w:][\-\w\d.]*\s*=\s*(["']).*?\3)*)\s*(\/)?>/mu>
Run Code Online (Sandbox Code Playgroud)
对此的要求是这样的:
REXML::Document.new(File.open(actual_file_name, "r"))
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何解决这个问题?
我对 Ruby 很陌生,并且尝试使用 REXML 解析 XML 文档,该文档之前已(通过 REXML)进行了漂亮的打印,但结果有点不稳定。
某些 CDATA 部分在开始 XML 标记之后、但在 CDATA 块开始之前有一个换行符,在这些情况下,REXML 会将标记的文本解析为空。
下面是一个 XML 文档示例(经过大量删节):
<?xml version="1.0" encoding="utf-8"?>
<root-tag>
<content type="base64"><![CDATA[V2VsbCBkb25lISBJdCB3b3JrcyA6KQ==]]></content>
<content type="base64">
<![CDATA[VGhpcyB3b250IHdvcms=]]></content>
<content><![CDATA[This will work]]></content>
<content>
<![CDATA[This will not appear]]></content>
<content>
Seems happy</content>
<content>Obviously no problem</content>
</root-tag>
Run Code Online (Sandbox Code Playgroud)
这是我的 Ruby 脚本(精简为一个最小的示例):
require 'rexml/document'
require 'base64'
include REXML
module RexmlSpike
file = File.new("ex.xml")
doc = Document.new file
doc.elements.each("root-tag/content") do |contentElement|
if contentElement.attributes["type"] == "base64"
puts "decoded: " << Base64.decode64(contentElement.text) …
Run Code Online (Sandbox Code Playgroud) 我想在所有XHTML段落的末尾修剪尾随空格.我正在使用Ruby和REXML库.
假设我在有效的XHTML文件中有以下内容:
<p>hello <span>world</span> a </p>
<p>Hi there </p>
<p>The End </p>
Run Code Online (Sandbox Code Playgroud)
我想最终得到这个:
<p>hello <span>world</span> a</p>
<p>Hi there</p>
<p>The End</p>
Run Code Online (Sandbox Code Playgroud)
所以我在想我可以使用XPath来获取我想要的文本节点,然后修剪文本,这样我就可以得到我想要的东西了(之前的).
我从以下XPath开始:
//root/p/child::text()
Run Code Online (Sandbox Code Playgroud)
当然,这里的问题是它返回所有p标签的子节点的所有文本节点.这是:
'hello '
' a '
'Hi there '
'The End '
Run Code Online (Sandbox Code Playgroud)
尝试以下XPath为我提供了最后一个段落的最后一个文本节点,而不是每个段落的最后一个文本节点,它是根节点的子节点.
//root/p/child::text()[last()]
Run Code Online (Sandbox Code Playgroud)
这只会返回: 'The End '
因此,我想从XPath获得的是:
' a '
'Hi there '
'The End '
Run Code Online (Sandbox Code Playgroud)
我可以用XPath做到这一点吗?或者我应该考虑使用正则表达式(这可能比XPath更令人头疼)?
rexml ×10
ruby ×10
xml ×5
cdata ×1
html ×1
pretty-print ×1
unit-testing ×1
xhtml ×1
xml-parsing ×1
xpath ×1
yaml ×1