简体   繁体   English

如何使用Nokogiri解析返回的XML

[英]How to parse this returned XML with Nokogiri

I'm attempting to parse this XML with nokogirl but I'm having trouble. 我试图用nokogirl解析此XML,但是遇到了麻烦。 Any ideas where I'm going wrong? 有什么想法我要去哪里吗? I'd like to get each Dealer and get the values for each of them. 我想得到每个经销商,并获取它们各自的价值。

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

returned XML: 返回的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>

Previously I've had a response like this: 以前我有这样的回应:

    <?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>

Which parses really easily with nokogiri: 使用nokogiri很容易解析:

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

I want to do something like: 我想做类似的事情:

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

To see what's wrong with a document use the errors method. 要查看文档出了什么问题,请使用errors方法。 After parsing your XML: 解析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>]

To extract the data I'd use something like this: 要提取数据,我将使用以下方法:

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"}]

Of course you'll want to add/remove/change fields being extracted to fit your use-case. 当然,您需要添加/删除/更改要提取的字段以适合您的用例。

Using slop mode has its dangers, as stated by the Nokogiri documentation . 利用slop模式有其危险性,如通过规定的引入nokogiri文档

  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! 您已被警告!

I've never used it as a result. 因此,我从未使用过它。 Often we don't want to use remove_namespaces! 通常,我们不想使用remove_namespaces! either, but it appears safe in your situation. 两者之一,但在您的情况下看起来很安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM