如何使用 Nokogiri 解析返回的 XML

How to parse this returned XML with Nokogiri

我正在尝试使用 nokogirl 解析此 XML,但我遇到了问题。我哪里出错了?我想获取每个 Dealer 并获取每个经销商的值。

doc = Nokogiri::Slop(response.body)
puts doc.content #works, shows the response below
puts doc.DTX_LEAD_ID.content #errors, no method found.
puts doc.NEWCAR_PINGGX_RESPONSE.content #errors, no method found

返回 XML:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="www.example.com/">
<?xml version="1.0" encoding="utf-8"?>
<NEWCAR_PINGGX_RESPONSE xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="www.example.com/SellerMessages">
    <DTX_LEAD_ID>1779853194</DTX_LEAD_ID>
    <SUCCESS>true</SUCCESS>
    <CACHED_RESPONSE>false</CACHED_RESPONSE>
    <PRICE>20</PRICE>
    <DealerList>
        <Dealer>
            <BUYER_ID>0000-2127</BUYER_ID>
            <Reservation_ID>1779853194|0000-2067|520a8037-57c8-497e-be4b-f4ea8dfa6c6f|14187-20</Reservation_ID>
            <Price>20</Price>
            <Name>Randy's Rides</Name>
            <State>MI</State>
            <City>Southfield</City>
            <Street>2001 Town Center</Street>
            <Postalcode>48076</Postalcode>
            <Distance>2.56002068066733</Distance>
            <DealerGroup id="2067" max_post="5" />
            <Contact><Name>John Campbell</Name>
            <Phone>2483521314</Phone>
            </Contact>
        </Dealer>
    </DealerList>
</NEWCAR_PINGGX_RESPONSE></string>

之前我收到过这样的回复:

    <?xml version="1.0" encoding="utf-8"?>
    <results>
      <status>accepted</status>
      <id>1724128693</id>
      <purchaseprice>8.0000</purchaseprice>
      <error>false</error>
      <messages>
        <message>coverage available</message>
      </messages>
    </results>

用 nokogiri 很容易解析:

doc.results.messages.message.content #coverage available

我想做这样的事情:

doc.NEWCAR_PINGGX_RESPONSE.DealerList.Dealer.Name.content #returns "Randy's Rides"

要查看文档有什么问题,请使用 errors 方法。在解析你的 XML:

doc.errors
# => [#<Nokogiri::XML::SyntaxError: xmlns: URI www.example.com/ is not absolute>,
#     #<Nokogiri::XML::SyntaxError: XML declaration allowed only at the start of the document>,
#     #<Nokogiri::XML::SyntaxError: xmlns: URI www.example.com/SellerMessages is not absolute>]

要提取数据,我会使用这样的东西:

doc = Nokogiri::XML(XML)
doc.remove_namespaces!
dealers = doc.search('Dealer').map{ |dealer|
  {
    buyer_id:       dealer.at( 'BUYER_ID'       ).text,
    reservation_id: dealer.at( 'Reservation_ID' ).text,
    name:           dealer.at( 'Name'           ).text
  }
}

dealers
# => [{:buyer_id=>"0000-2127",
#      :reservation_id=>
#       "1779853194|0000-2067|520a8037-57c8-497e-be4b-f4ea8dfa6c6f|14187-20",
#      :name=>"Randy's Rides"},
#     {:buyer_id=>"0000-2127",
#      :reservation_id=>
#       "1779853194|0000-2067|e42fd5c6-0a36-4552-8b6a-ad2decebd0db|14200-10",
#      :name=>"Jarrett's New Car Dealership 01"},
#     {:buyer_id=>"0000-2127",
#      :reservation_id=>
#       "1779853194|0000-2067|3fecb591-3a81-49f9-82b3-1f0d7fb3f7a6|14160-20",
#      :name=>"Campbell's Crazy Cars"},
#     {:buyer_id=>"0000-2127",
#      :reservation_id=>
#       "1779853194|0000-2067|731b09e9-700b-4f41-8cb0-eaf80e861d76|14158-7",
#      :name=>"Demo Dealer 3"}]

当然,您会希望 add/remove/change 提取字段以适合您的用例。

使用 slop 模式有其危险,如 the Nokogiri documentation 所述。

  1. Don’t use this.
  2. This may or may not be a backhanded compliment.
  3. No, really, don’t use this. If you use it, don’t report bugs.
  4. You’ve been warned!

因此我从未使用过它。通常我们也不想使用 remove_namespaces!,但在您的情况下它似乎是安全的。