当我尝试发出json post请求时出现以下错误:
REXML::ParseException (The document "{\"user\":1,\"email\":\"some@email.com\"}:" does not have a valid root):
Run Code Online (Sandbox Code Playgroud)
不知道从哪里开始.
我的标题看起来像这样:
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:38
Content-Type:application/x-www-form-urlencoded
Cookie:_washapp_session=SOME_COOKIE_STRING
Host:myapp.herokuapp.com
Origin:http://myapp.herokuapp.com
Referer:http://myapp.herokuapp.com/user/1/add_friends
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5
X-CSRF-Token:k5bxjBJ3SCMhe1ILfChoiy6qwkYfVsg0JYZV9KDnwIg=
X-Requested-With:XMLHttpRequest
Run Code Online (Sandbox Code Playgroud) Hi am trying to parse an XML file using REXML .... when there is an illegal character in my XML file ...its jus fails at this point.
So is there any way we could replace or remove these kind of characters ?
无法解析原始字符串REXML解析中的错误Illegal character'&'
<head> Negative test for underlying BJSPRICEENG N4&N5
</head>
doc = REXML::Document.new(File.open(file_name,"r:iso-8859-1:utf-8"))
testfile.elements["head"].text
doc = REXML::Document.new(content)
dir_path = doc.elements["TestBed/TestDir"].attributes["path"].to_s
doc.elements.each("TestBed/TestDir") do |directory|
directory.elements.each("file") do |testfile|
t= testfile.elements["head"].text
end
end
end
<file name="toptstocksensbybjs.m">
<MCheck></MCheck>
<TestExtension></TestExtension>
<TestType></TestType>
<fcn name="lvlTwoDocExample" …Run Code Online (Sandbox Code Playgroud) 我使用的是 Ruby 版本 1.9.3。这是我想要从中获取信息的实际 XML 页面的简单版本。我需要从需要登录凭据的安全网站访问它。我无法使用 Nokogiri,因为我无法使用它登录网站。
<root>
<person>
<name>Jack</name>
<age>10</age>
</person>
<person>
<name>Jones</name>
</person>
<person>
<name>Jon</name>
<age>16</age>
</person>
</root>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,有时该标签age不会出现。将 REXML 与 Ruby 结合使用,我使用以下代码:
agent = Mechanize.new
xml = agent.get("https://securewebsite.com/page.xml")
document = REXML::Document.new(xml.body)
name = XPath.match(document, "//person/name").map {|x| x.text}
# => ["Jack", "Jones", "Jon"]
age = XPath.match(document, "//person/age").map {|x| x.text}
# => ["10", "16"]
Run Code Online (Sandbox Code Playgroud)
问题是我无法将age与正确的关联起来name,因为索引现在乱序了。例如,在索引 1 处,name[1] 是 Jones,但age[1] 是 16。但这不是真的,因为personJones 的标签没有年龄标签。
有什么方法可以让数组age输出:# => ["10", nil ,"16"]以便我可以将正确的名字与其相应的年龄相关联? …
当我查看XML文件时,它看起来很好,并开始 <?xml version="1.0" encoding="utf-16le" standalone="yes"?>
但是当我在Ruby中读取它并将其打印到粗壮时,前面有两个: ??<?xml version="1.0" encoding="utf-16le" standalone="yes"?>
这些来自哪里,如何删除它们?使用REXML解析它会立即失败.删除第一个字符,然后解析它,给我这个错误:
REXML::ParseException: #<REXML::ParseException: malformed XML: missing tag start
Line:
Position:
Last 80 unconsumed characters:
<?xml version="1.0" encoding="utf-16le" s>
处理这个问题的正确方法是什么?
编辑:下面是我的代码.该ftp.get下载从FTP服务器的XML.(我想知道这可能是相关的.)
xml = ftp.get
puts xml
until xml[0,1] == "<" # to remove the 2 invalid characters
puts xml[0,2]
xml.slice! 0
end
puts xml
document = REXML::Document.new(xml)
Run Code Online (Sandbox Code Playgroud)
最后一个put打印出正确的xml.但由于这两个无效的角色,我感觉还有其他问题.没有必要删除任何东西.不过,我不知道问题可能是什么.
编辑2:我正在使用Net :: FTP下载XML,但是这个新方法让我可以将内容读入字符串而不是文件:
class Net::FTP
def gettextcontent(remotefile, &block) # :yield: line
f = StringIO.new()
begin
retrlines("RETR " + remotefile) …Run Code Online (Sandbox Code Playgroud)