From: Robert Klemme Date: 2009-12-01T03:45:08+09:00 Subject: Re: Odd puts behaviour with REXML On 30.11.2009 19:33, John W Higgins wrote: > [Note: parts of this message were removed to make it a legal post.] > > Morning David, > > On Mon, Nov 30, 2009 at 10:19 AM, David Sainte-claire < > dsainteclaire@gmail.com> wrote: > >> Hello, >> >> This is really weird, and I'm not sure if maybe there's a formatting >> option that I can set on the puts method that will solve it. Hopefully >> someone has done something similar?? >> >> I have been using REXML to parse through an XML document that I get back >> from calling a ReST-ish web service, and up until now it has parsed >> every single element as I'd expect. But I run into this small patch of >> data were it acts weird. The XML looks like this: >> >> >> >> eSupport >> Find related product support and help in >> >> >> http://www.somecompany.com/US/perl/model-home.pl?XID=M:products:crmportal&LOC=3&mdl=someModel >> >> >> >> >> And I'm parsing it like this: >> data.elements.each("product/profile/support"){|element| >> element.elements.each() do |child| >> puts "Did XPATH match any profile elements?" >> puts child.text >> end >> } >> >> I know it's pretty rudimentary, but I'm just confused as to why this >> same pattern has been able to get text out of every other element I have >> searched for, but here I get something like this when I loop through the >> subelemenents: >> Did XPATH match any profile elements? >> ???Product_Support_Link_Title??? >> Did XPATH match any profile elements? >> ???Product_Support_Link_Prefix??? >> Did XPATH match any profile elements? >> ???Product_Support_Link???&mdl=someModel >> >> It looks like XPATH doesn't like the format of the text somehow, but I >> have no idea what it is about it that it doesn't like since it appears >> to be just normal text to me. >> >> Anyone have any ideas? It looks like something that REXML is doing >> since it outputs the path to the element in the document Product --> >> Support but I don't have a clue where the _Link is coming from? >> >> > Rule one of anything like this is to remove each element from the XML file > one by one and see which one is breaking you. In this case you will find > that it's the link element because you are using ampersands in the URL. See > here > > http://www.w3.org/TR/xhtml1/guidelines.html#C_12 One more hint: when posting things like this it is best to provide a _complete_ example. From the XPath given it is clear that something must be missing (there are no "product" tags). It's pretty easy with REXML: robert@fussel ~ $ cat x.rb require 'rexml/document' data = REXML::Document.new <<'DOC' eSupport Find related product support and help in http://www.somecompany.com/US/perl/model-home.pl?XID=M:products:crmportal&LOC=3&mdl=someModel DOC data.elements.each("product/profile/support"){|element| element.elements.each() do |child| puts "Did XPATH match any profile elements?" puts child.text end } Which on my box produces: robert@fussel ~ $ ruby19 x.rb /usr/local/lib/ruby19/1.9.1/rexml/parsers/treeparser.rb:95:in `rescue in parse': # /usr/local/lib/ruby19/1.9.1/rexml/text.rb:155:in `block in check' /usr/local/lib/ruby19/1.9.1/rexml/text.rb:153:in `scan' /usr/local/lib/ruby19/1.9.1/rexml/text.rb:153:in `check' /usr/local/lib/ruby19/1.9.1/rexml/text.rb:125:in `parent=' /usr/local/lib/ruby19/1.9.1/rexml/parent.rb:19:in `add' /usr/local/lib/ruby19/1.9.1/rexml/parsers/treeparser.rb:45:in `parse' /usr/local/lib/ruby19/1.9.1/rexml/document.rb:228:in `build' /usr/local/lib/ruby19/1.9.1/rexml/document.rb:43:in `initialize' x.rb:4:in `new' x.rb:4:in `
' ... Illegal character '&' in raw string " http://www.somecompany.com/US/perl/model-home.pl?XID=M:products:crmportal&LOC=3&mdl=someModel " Line: 8 Position: 221 Last 80 unconsumed characters: from /usr/local/lib/ruby19/1.9.1/rexml/parsers/treeparser.rb:20:in `parse' from /usr/local/lib/ruby19/1.9.1/rexml/document.rb:228:in `build' from /usr/local/lib/ruby19/1.9.1/rexml/document.rb:43:in `initialize' from x.rb:4:in `new' from x.rb:4:in `
' robert@fussel ~ $ How did you manage to get REXML parse this? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/