mvz / happymapper

Object to XML mapping library, using Nokogiri (Fork from John Nunemaker's Happymapper)
http://github.com/mvz/happymapper/
MIT License
152 stars 44 forks source link

Is it possible to parse such xml with HappyMapper? #57

Closed zekefast closed 6 years ago

zekefast commented 9 years ago

I have follow XML:

<?xml version="1.0"?>
<a:feed xmlns:a="http://www.w3.org/2005/Atom" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://schemas.zune.net/catalog/apps/2008/02">
  <a:link rel="self" type="application/atom+xml" href="/v9/catalog/apps/27f2e0ef-5a0c-4dca-a383-b89f9485b9d2?os=8.10.12393.0&amp;cc=US&amp;lang=en-US"/>
  <a:updated>2015-04-14T12:58:30.328606Z</a:updated>
  <a:title type="text">Beach Buggy Racing</a:title>
  <a:id>urn:uuid:27f2e0ef-5a0c-4dca-a383-b89f9485b9d2</a:id>
  <a:content type="html">Beach Buggy is back with an explosive sequel! Drive into an action-packed, surprise-filled world of off-road kart racing mayhem. Race against a field of rival drivers, each with unique personalities and special abilities. Build a collection of crazy powerups, like Dodgeball Frenzy, Fireball, and Oil Slick. Unlock and upgrade a variety of cars, from dune buggies to monster trucks. Test your skills in 6 different game modes on 15 imaginative 3D race tracks, against a pack of tropical-loving rivals with a serious case of road rage!

This is the official sequel Beach Buggy Blitz, the free driving game with over 30 Million players worldwide. Fast, furious, fun and FREE, Beach Buggy Racing is a kart-racing island adventure for all ages.

&#x2022; &#x2022; GAME FEATURES &#x2022; &#x2022;

EXCITING KART-RACING ACTION
Utilize your driving skills and a collection of creative powerups to fight your way to the finish line. It&#x2019;s not just a great looking 3D racing game, it&#x2019;s an epic battle with spectacular physics-based gameplay!

COOL CARS TO CUSTOMIZE
Use your winnings to collect and upgrade a garage full of unique cars, from monster trucks to muscle cars to lunar rovers!

TONS OF AMAZING POWERUPS
Beach Buggy Racing crushes other kart racers with over 25 totally unique Powerups ... and more Powerups are coming!

15 SPECTACULAR RACE TRACKS
Explore dinosaur-infested jungles, lava-spewing volcanoes, beautiful beaches, and mysterious swamps. Each unique race track is packed with hidden shortcuts and surprises.

COLLECT A TEAM OF RACERS
Recruit a team of drivers to play with, each with a unique special power like teleportation, flaming fire tracks, and confusion spells.

PLAY THE WAY YOU WANT
Chose between multiple control options, and customize the 3D graphics settings to optimize your play experience.

&#x2022; &#x2022; CUSTOMER SUPPORT &#x2022; &#x2022;

If you encounter a problem running the game, please email us at support@vectorunit.com. Be sure to include the device you're using, OS version, and a detailed description of your problem.

For fast support on most common issues please visit:
www.vectorunit.com/support

&#x2022; &#x2022; MORE INFORMATION &#x2022; &#x2022;

Be the first to hear about updates, download custom images, and interact with the developers!
Follow us on Google+ at www.vectorunit.com/+
Like us on Facebook at www.facebook.com/VectorUnit
Follow us on Twitter @vectorunit.
Visit our web page at www.vectorunit.com</a:content>
  <iapCount>13</iapCount>
  <userRatingCountDistributions>
    <oneStarRatings>11</oneStarRatings>
    <twoStarRatings>3</twoStarRatings>
    <threeStarRatings>5</threeStarRatings>
    <fourStarRatings>11</fourStarRatings>
    <fiveStarRatings>70</fiveStarRatings>
  </userRatingCountDistributions>
  <sortTitle>Beach Buggy Racing</sortTitle>
  <releaseDate>2014-11-03T17:30:21.417000Z</releaseDate>
  <whatsNew>v1.2.3
- Fixed crash when initiating IAPs

v1.2.2
- Fixed low resolution issue
- Fixed championship continuation issue
- Fixed touch steering responsiveness near screen edge

v1.2.1
- Fixed localization to German, Spanish, Russian, and Trad Chinese

v1.2
- 3 new tracks:  Fire &amp; Ice, Aquarius, Spooky Shores
- Championship Mode - Win gold-plated cars!
- Quick Race Mode - Simply pick car/driver/track and race!
- Win Power-Ups with new Coco Loco mini-game 
- Localized to German, Spanish, Russian, and Trad Chinese
- New Achievements
- Lots of Tweaks/Tuning</whatsNew>
  <visibilityStatus>Live</visibilityStatus>
  <publisherUrl>http://www.vectorunit.com/beach-buggy-racing</publisherUrl>
  <publisher>Vector Unit</publisher>
  <privacyPolicyUrl>http://www.vectorunit.com/privacy</privacyPolicyUrl>
  <averageUserRating>8.499456</averageUserRating>
  <userRatingCount>1838</userRatingCount>
  <image>
    <id>urn:uuid:f960a7db-890a-4157-a47d-261b3499b096</id>
  </image>
  <screenshots>
    <screenshot>
      <id>urn:uuid:1ddcf43c-1176-473b-bf0c-1f30c65a12a5</id>
      <orientation>90</orientation>
    </screenshot>
    <screenshot>
      <id>urn:uuid:2a6cff2f-0790-4005-abaf-0b8dbb79ce31</id>
      <orientation>90</orientation>
    </screenshot>
    <screenshot>
      <id>urn:uuid:9980a307-3793-4c60-b20f-8c3853ab9c47</id>
      <orientation>90</orientation>
    </screenshot>
    <screenshot>
      <id>urn:uuid:4ba0b8ae-dfd8-4e11-b9d1-18920dbf6e04</id>
      <orientation>90</orientation>
    </screenshot>
  </screenshots>
  <doubleWideImage>
    <id>urn:uuid:19e41422-cde5-41d3-b5e1-963a1abf02d5</id>
  </doubleWideImage>
  <squareImage>
    <id>urn:uuid:aca3e40a-3846-4f74-be87-fd1639282c34</id>
  </squareImage>
  <categories>
    <category>
      <id>windowsphone.Games</id>
      <title>games</title>
      <isRoot>True</isRoot>
    </category>
    <category>
      <id>windowsphone.RacingAndFlying</id>
      <title>racing + flying</title>
      <isRoot>False</isRoot>
      <parentId>windowsphone.Games</parentId>
    </category>
  </categories>
  <tags>
    <tag>Independent</tag>
  </tags>
  <offers>
    <offer>
      <offerId>urn:uuid:6eba4bfa-e9bf-47a2-869b-aa75e6317e19</offerId>
      <mediaInstanceId>urn:uuid:23deb049-4f8b-4041-8d52-b11d0ca2db21</mediaInstanceId>
      <clientTypes>
        <clientType>WindowsPhone81</clientType>
      </clientTypes>
      <paymentTypes>
        <paymentType>Credit Card</paymentType>
        <paymentType>Mobile Operator</paymentType>
      </paymentTypes>
      <store>ZEST</store>
      <price>0</price>
      <displayPrice>$0.00</displayPrice>
      <priceCurrencyCode>USD</priceCurrencyCode>
      <licenseRight>Purchase</licenseRight>
      <expiration>2100-01-01T00:00:00Z</expiration>
    </offer>
  </offers>
  <taxString>plus applicable taxes</taxString>
  <backgroundImage>
    <id>urn:uuid:93e13edb-c38f-49cb-aeb5-896302bbe1f7</id>
  </backgroundImage>
  <publisherId>Vector Unit</publisherId>
  <publisherGuid>urn:uuid:9841e024-61d1-422d-87cc-61e8012632d3</publisherGuid>
  <a:entry>
    <a:updated>2015-04-14T12:58:30.328606Z</a:updated>
    <a:title type="text">Beach Buggy Racing 2015.127.2334.5274</a:title>
    <a:id>urn:uuid:23deb049-4f8b-4041-8d52-b11d0ca2db21</a:id>
    <version>2015.127.2334.5274</version>
    <payloadId>urn:uuid:0a683af8-0d83-4171-b7e0-10bc986f7252</payloadId>
    <skuId>urn:uuid:23deb049-4f8b-4041-8d52-b11d0ca2db21</skuId>
    <skuLastUpdated>2015-02-06T22:58:16.790000Z</skuLastUpdated>
    <isAvailableInCountry>true</isAvailableInCountry>
    <isAvailableInStore>true</isAvailableInStore>
    <isClientTypeCompatible>true</isClientTypeCompatible>
    <isHardwareCompatible>true</isHardwareCompatible>
    <isBlacklisted>false</isBlacklisted>
    <url>http://cdn.marketplacecontent.windowsphone.com/public/9cfb7496-3060-47d2-955b-c23369d9f778</url>
    <packageSize>92904030</packageSize>
    <installSize>95898624</installSize>
    <estimatedDownloadSize>92841984</estimatedDownloadSize>
    <packageFormat>AppXBundle</packageFormat>
    <packageFullName>VectorUnit.BeachBuggyRacing_2015.127.2334.5274_neutral_~_hvbhrzr8672s2</packageFullName>
    <isFramework>false</isFramework>
    <dependencyPackages>
      <dependencyPackage>
        <id>urn:uuid:5dec6302-3af3-4a4b-bc6e-d800779376b9</id>
        <skuId>urn:uuid:1076f542-8a6f-4cda-86e2-012bb248c9ef</skuId>
        <url>http://cdn.marketplacecontent.windowsphone.com/public/bdbd4abf-a3ab-433c-be54-0ec0278f8f49</url>
        <packageSize>645282</packageSize>
        <installSize>1544192</installSize>
        <version>12.0.30113.0</version>
        <packageFullName>Microsoft.VCLibs.120.00.Phone_12.0.30113.0_arm__8wekyb3d8bbwe</packageFullName>
      </dependencyPackage>
    </dependencyPackages>
    <isUniversal>true</isUniversal>
    <supportedPlatforms>Get once, download on compatible Windows devices too</supportedPlatforms>
    <clientTypes>
      <clientType>WindowsPhone81</clientType>
    </clientTypes>
    <supportedLanguages>
      <supportedLanguage>Deutsch</supportedLanguage>
      <supportedLanguage>English</supportedLanguage>
      <supportedLanguage>espa&#xF1;ol</supportedLanguage>
      <supportedLanguage>&#x440;&#x443;&#x441;&#x441;&#x43A;&#x438;&#x439;</supportedLanguage>
      <supportedLanguage>&#x4E2D;&#x6587;(&#x7E41;&#x9AD4;)</supportedLanguage>
    </supportedLanguages>
    <deviceCapabilities>&lt;hwCapability&gt;&lt;requirementType&gt;Resolution&lt;/requirementType&gt;&lt;id&gt;ID_RESOLUTION_HD720P&lt;/id&gt;&lt;string&gt;HD720P (720x1280)&lt;/string&gt;&lt;required&gt;true&lt;/required&gt;&lt;/hwCapability&gt;&lt;hwCapability&gt;&lt;requirementType&gt;Resolution&lt;/requirementType&gt;&lt;id&gt;ID_RESOLUTION_WVGA&lt;/id&gt;&lt;string&gt;WVGA (480x800)&lt;/string&gt;&lt;required&gt;true&lt;/required&gt;&lt;/hwCapability&gt;&lt;hwCapability&gt;&lt;requirementType&gt;Resolution&lt;/requirementType&gt;&lt;id&gt;ID_RESOLUTION_WXGA&lt;/id&gt;&lt;string&gt;WXGA (768x1280)&lt;/string&gt;&lt;required&gt;true&lt;/required&gt;&lt;/hwCapability&gt;&lt;capability&gt;&lt;id&gt;internetClientServer&lt;/id&gt;&lt;string&gt;internet connection&lt;/string&gt;&lt;disclosure&gt;Disclose&lt;/disclosure&gt;&lt;/capability&gt;</deviceCapabilities>
  </a:entry>
  <a:author>
    <a:name>Microsoft Corporation</a:name>
  </a:author>
  <isUniversal>true</isUniversal>
  <supportedPlatforms>Get once, download on compatible Windows devices too</supportedPlatforms>
</a:feed>

All elements with namespace a parsed as expected with follow definition:

require "happymapper"

class AppMapper
  include ::HappyMapper

  namespace "a"
  tag       "feed"

  element :updated,             DateTime
  element :title,               String
  element :id,                  String
  element :description,         String,   tag: "content"
  element :publisher_url,       String,   tag: "publisherUrl", namespace: nil
end

But publisher_url field wasn't parsed.

I tried different combinations of xpath, tag, namespace options, but neither is working. When I load this document to Nokogiri. I was able to access element with follow xpath expression: /a:feed/xmlns:publisherUrl

xml = File.read("/tmp/27f2e0ef-5a0c-4dca-a383-b89f9485b9d2.xml")
doc = ::Nokogiri::XML(xml)
doc.xpath("/a:feed/xmlns:publisherUrl").text # => "http://www.vectorunit.com/beach-buggy-racing"

Versions: ruby: 2.2.1 nokogiri: 1.6.6.2 nokogiri-happymapper: 0.5.9

Does anyone know whether it possible to map such xml using nokogiri-happymapper?

fidalgo commented 6 years ago

@zekefast Better ask on StackOverflow

burtlo commented 6 years ago

If I remember correctly, it's hard for the elements defined in the class to shake the namespace that was given to them. It's most likely a bug. So you can get around that by creating another class for the element.

require "happymapper"

class PublisherUrl
  include ::HappyMapper

  content :url, String
end

class AppMapper
  include ::HappyMapper

  namespace "a"
  tag       "feed"

  element :updated,             DateTime
  element :title,               String
  element :id,                  String
  element :description,         String,   tag: "content"
  # element :publisher_url,       String,   tag: "publisherUrl", namespace: nil

  def publisher_url
    publisher_url_object.url
  end
  element :publisher_url_object,       PublisherUrl,   tag: "publisherUrl"
end

app = AppMapper.parse(File.read('buggy.xml'))
puts "Publisher #{app.publisher_url}"
burtlo commented 6 years ago

Oh yeah, here is the issue: https://github.com/mvz/happymapper/issues/43