简体   繁体   English

使用Nokogiri / XPath进行Ruby XML解析

[英]Ruby XML Parsing with Nokogiri/XPath

I have a shopify store that I want to automatically update the product variants inventory levels with, using a live xml feed from the wholesaler I use. 我有一个shopify商店,我想使用我使用的批发商提供的实时xml提要自动更新产品型号的库存水平。

I'm learning to program (Ruby) and this is my first project, but after researching here is how I think it should work. 我正在学习编程(Ruby),这是我的第一个项目,但是在这里进行研究后,我认为它应该如何工作。

Use Ruby/Nokugiri to parse the XML feed from the wholesaler, and then Xpath to locate both the unique product variant SKU code, and the stock level. 使用Ruby / Nokugiri解析批发商的XML提要,然后使用Xpath定位唯一的产品变体SKU代码和库存水平。

Somehow I need to use this SKU to refer back to my Shopify store product XML list, and pull out the variants unique ID using the SKU code. 我需要以某种方式使用此SKU来参考我的Shopify商店产品XML列表,并使用SKU代码提取出变体的唯一ID。

Then use something like the builder gem to build the XML format that shopify needs, and then use curl to PUT the changes. 然后使用诸如builder gem之类的东西来构建满足Shopify需求的XML格式,然后使用curl来放置更改。 I'm guessing I loop this process for every product? 我猜我会为每个产品循环执行此过程吗?

I know Shopify only has a 300 call limit, so I've got the article on putting a delay in the script, but I get the feeling the above method isn't the easiest way to go about this? 我知道Shopify的通话限制只有300个,所以我有一篇文章介绍了在脚本中放置延迟的方法,但是我觉得上述方法不是解决此问题的最简单方法?

With Shopify you need to apply the variant stock level update against unique variant xml files, so I need to build the unique xml file/code and PUT it against /admin/variants/#[thevariantid].xml 使用Shopify,您需要对唯一的变体xml文件应用变体库存水平更新,因此我需要构建唯一的xml文件/代码,然后将其放入/admin/variants/#[thevariantid].xml

I'm looking forward to trying to put this together and learning in the process, but am I on the right track with this? 我期待着将其整合在一起并在此过程中进行学习,但是我是否在正确的道路上? Are there simpler gems I should be looking at? 我应该看些更简单的宝石吗?

nb I've only recently started learning Ruby, and will head to Rails afterwards. nb我最近才开始学习Ruby,之后将前往Rails。 I know a bit about XML and it's structure so should be ok finding what I need with XPath. 我对XML及其结构有所了解,因此可以通过XPath找到我需要的东西。

You're on the right track, but I'd use the shopify_api gem to do the talking to Shopify instead of having to form the XML and URIs yourself: https://github.com/Shopify/shopify_api 您的方向正确,但我可以使用shopify_api gem与Shopify进行对话,而不必自己形成XML和URI: https : //github.com/Shopify/shopify_api

There's an article on our wiki that might also help you out with regards to the API call limit but just let me know if you need more space – we're pretty flexible and the limit is really just there to keep scripts from going wild and affecting service for everyone else. 我们的Wiki上一篇文章也可能会帮助您了解API调用限制,但请告诉我是否需要更多空间-我们非常灵活,而且限制确实存在,可以防止脚本发狂并影响为其他人服务。

您建议的路径看起来不错,只是不需要使用“ builder” gem,因为Nokogiri内置了一些非常不错的XML构建

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

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