编辑:XML通过curl
命令解析。它不喜欢的例子初始化我有一个XML字符串
xml = <?xml version=1.0 encoding=UTF-8 standalone=yes?>
<Document xmlns=somexmlns>
<tag> some data </tag>
</Document>
当我尝试用引入nokogiri解析,(我尝试了所有3)
Nokogiri::XML(xml)
Nokogiri::XML.parse(xml)
Nokogiri::XML.parse(xml).remove_namespaces!
我得到
<?xml version="1.0"?>
<Document/>
我知道有引入nokogiri解析它之前,我可以删除xmlnx
标签,但我想知道这是为什么发生,我怎么能解决这个问题,而无需修改XML本身。
有用:
doc = Nokogiri::XML ('<?xml version="1.0" encoding="UTF-8"?><document xmlns="somexmlns"><tag> some data </tag></document>')
=> #<Nokogiri::XML::Document:0x3fda1d0c9dd8 name="document" children=[#<Nokogiri::XML::Element:0x3fda1d0c9b1c name="document" namespace=#<Nokogiri::XML::Namespace:0x3fda1d0c9acc href="somexmlns"> children=[#<Nokogiri::XML::Element:0x3fda1d0c9770 name="tag" namespace=#<Nokogiri::XML::Namespace:0x3fda1d0c9acc href="somexmlns"> children=[#<Nokogiri::XML::Text:0x3fda1d0c93ec " some data ">]>]>]>
doc.children.text
=> " some data "
这是所有关于报价。你为什么不使用它们?
不幸的是,从the standard报价不正确地插入(因为引号的),所以我把它写下来作为代码。
# To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " ' ", and the double-quote character (") as " " ".
看:
xml = <<-BADXML
<?xml version=1.0 encoding=UTF-8 standalone=yes?>
<Document xmlns=somexmlns>
<tag> some data </tag>
</Document>
BADXML
doc = Nokogiri::XML(xml)
puts doc
# <?xml version="1.0"?>
# <Document/>
xml = <<-GOODXML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document xmlns="somexmlns">
<tag> some data </tag>
</Document>
GOODXML
doc = Nokogiri::XML(xml)
puts doc
# <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
# <Document xmlns="somexmlns">
# <tag> some data </tag>
# </Document>
此外,最好使用小写字母像<document></document>
原来,我忘了设定的数据类型为卷曲text/xml
。