使用 PHP 和 PHP 解析从 OPS 专利数据库返回的复杂 XML简单XML

发布于 2024-12-03 05:14:03 字数 914 浏览 0 评论 0原文

我一直对 SimpleXML 着迷,试图将值放入可用的 PHP 变量中,这让我发疯。

我真诚地希望你们中一些更有才华的程序员能够帮助我...... 我将尽可能彻底...

我正在使用开放专利服务 API。使用以下 URL,我可以轻松生成包含我需要的所有数据的格式化 XML 文件。

<?php

// Patent Reference Number
$ref = "EP2359415";

// URL for XML response
$url =  "http://ops.epo.org/2.6.2/rest-services/published-data/publication/epodoc/".$ref."/biblio";

// Reading the XML Response
$sitemap = new SimpleXMLElement($url);

// Echo out values from the XML Data
foreach($needhelp as $here) {
   echo "Need Help Here!";
   // Will be taking data and placing into a database here...
 } ?>

如果您看到该网址... http://ops.epo.org /2.6.2/rest-services/published-data/publication/epodoc/EP2359415/biblio

您将看到返回的 XML 有多复杂 是。 基本上我无法通过 php 循环从数据中获取任何值...

任何帮助将不胜感激... 院长

I have been going crazy with SimpleXML trying to get values into usable PHP variables and it's driving me insane.

I am genuinely hoping some of you more talented coders out there can help me out...
I will be as thorough as I can...

I am using the Open Patent Service API. Using the following URL I can easily generate a formatted XML file with all the Data I need.

<?php

// Patent Reference Number
$ref = "EP2359415";

// URL for XML response
$url =  "http://ops.epo.org/2.6.2/rest-services/published-data/publication/epodoc/".$ref."/biblio";

// Reading the XML Response
$sitemap = new SimpleXMLElement($url);

// Echo out values from the XML Data
foreach($needhelp as $here) {
   echo "Need Help Here!";
   // Will be taking data and placing into a database here...
 } ?>

If you see the URL...
http://ops.epo.org/2.6.2/rest-services/published-data/publication/epodoc/EP2359415/biblio

You will see how complicated the XML returned is.
Basically I cannot get any values out of the data via php loops...

Any help would be greatly appreciated...
Dean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻梦 2024-12-10 05:14:03

我知道这是一个老问题,但我永远无法让 SimpleXML 做任何事情。鉴于这是在 Google 搜索中出现的关于在 PHP 中使用欧洲专利 Office API 的唯一内容,我想我应该记录一下对我有用的内容...

以下是我解决它的方法:

# build query url
$patent_url = 'http://ops.epo.org/3.0/rest-services/published-data/search/full-cycle/?q='.urlencode($your_query);

# grab the contents of $patent_url
$patent_raw = file_get_contents($patent_url);

# create an XML parser
$resource = xml_parser_create();

# parse XML into array 
xml_parse_into_struct($resource, $patent_raw, $patent_array);

# close the parser - you want to do this...    
xml_parser_free($resource);

现在你有一个标准的 PHP 数组($patent_array) 您可以迭代。请注意,这与我的代码类似,但不完全相同 - 如果您剪切/粘贴,您可能需要对其进行调整...当然,您仍然需要弄清楚如何处理委员会设计的极其复杂的代码数据结构,但至少它是可变的形式。

编辑:

在尝试获得更复杂的结果时,很明显 EPO 数据不是严格的 XML。 SimpleXML&上面的代码在尝试解析结果时什么都不做。解决方案是使用容错的 DOM XML 解析器。我使用的代码如下所述: http://set.cooki .me/archives/225/php-dom-xml-to-array

I know this is an old question, but I could never get SimpleXML to do anything. Given that this is the only thing that comes up in a Google search about using the European Patent OFfice API with PHP, I thought I'd document what worked for me...

Here's how I solved it:

# build query url
$patent_url = 'http://ops.epo.org/3.0/rest-services/published-data/search/full-cycle/?q='.urlencode($your_query);

# grab the contents of $patent_url
$patent_raw = file_get_contents($patent_url);

# create an XML parser
$resource = xml_parser_create();

# parse XML into array 
xml_parse_into_struct($resource, $patent_raw, $patent_array);

# close the parser - you want to do this...    
xml_parser_free($resource);

Now you have a standard PHP array ($patent_array) you can iterate through. Note that this is similar to my code, but not exactly the same - you may have to tweak it if you cut/paste... Of course, you still have to figure out what to do with the ridiculously complex designed-by-committee data structure, but at least it's in mugable form.

Edit:

While trying to get more complex results, it became clear that EPO data is not strict XML. SimpleXML & the above code both do nothing when trying to parse results. The solution was to use a DOM XML parser, which is fault tolerant. The code I used is described here: http://set.cooki.me/archives/225/php-dom-xml-to-array

思念绕指尖 2024-12-10 05:14:03
$xml = simplexml_load_file($url);
$xml->registerXPathNamespace('os', $url);
foreach ($xml->children() as $child)
{
  // your insertion into database
}
$xml = simplexml_load_file($url);
$xml->registerXPathNamespace('os', $url);
foreach ($xml->children() as $child)
{
  // your insertion into database
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文