首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用PHP和SimpleXML解析运维专利数据库返回的复杂XML

用PHP和SimpleXML解析运维专利数据库返回的复杂XML
EN

Stack Overflow用户
提问于 2011-09-06 04:58:12
回答 2查看 742关注 0票数 3

我一直在疯狂地使用SimpleXML试图将值转换为可用的PHP变量,这让我发疯。

我真心希望你们中一些更有才华的程序员能帮助我……我会尽力而为的.

我正在使用Open Patent Service API。使用下面的URL,我可以轻松地生成一个格式化的XML文件,其中包含我需要的所有数据。

代码语言:javascript
复制
<?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...
 } ?>

如果你看到URL..。http://ops.epo.org/2.6.2/rest-services/published-data/publication/epodoc/EP2359415/biblio

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

任何帮助都将不胜感激。院长

EN

回答 2

Stack Overflow用户

发布于 2013-06-13 17:30:47

我知道这是一个老生常谈的问题,但我永远不能让SimpleXML做任何事情。鉴于这是谷歌搜索到的关于在OFfice中使用欧洲专利PHP的唯一内容,我想我应该记录一下什么对我有效……

下面是我解决这个问题的方法:

代码语言:javascript
复制
# 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)。请注意,这与我的代码类似,但并不完全相同-如果您剪切/粘贴...当然,您仍然需要弄清楚如何处理委员会设计的极其复杂的数据结构,但至少它是mugable形式的。

编辑:

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

票数 2
EN

Stack Overflow用户

发布于 2011-09-09 16:25:13

代码语言:javascript
复制
$xml = simplexml_load_file($url);
$xml->registerXPathNamespace('os', $url);
foreach ($xml->children() as $child)
{
  // your insertion into database
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7312685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档