如何解析 laravel 5 中的 USPTO xml 响应
How to parse USPTO xml response in laravel 5
我一直在尝试使用 laravel 解析来自 USPTO 作业 api 的响应,但一切似乎都是错误的。在此之前,我真的从来没有用 xml 做过这么多。
这是 xml 的示例:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">78</int>
<lst name="params">
<str name="fl">id,reelNo,frameNo,conveyanceText,patAssigneeName,patAssignorName,inventionTitleFirst,applNumFirst,publNumFirst,patNumFirst,intlRegNumFirst,corrName,corrAddress1,corrAddress2,corrAddress3,patAssignorEarliestExDate,filingDateFirst,publDateFirst,issueDateFirst,intlPublDateFirst,patNumSize,applNum,applNumSize,intlRegNum</str>
<str name="sort">patAssignorEarliestExDate desc</str>
<str name="start">0</str>
<str name="q">Ojo</str>
<str name="wt">javabin</str>
<str name="qt">/apisearch</str>
<str name="fq">applNum:(13096729)</str>
<str name="version">2</str>
<str name="rows">8</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="id">48719-909</str>
<str name="reelNo">48719</str>
<str name="frameNo">909</str>
<str name="conveyanceText">ASSIGNMENT OF ASSIGNORS INTEREST (SEE DOCUMENT FOR DETAILS).</str>
<str name="corrName">LOCKE LORD LLP</str>
<str name="corrAddress1">P.O. BOX 55874</str>
<str name="corrAddress2">BOSTON, MA 02205</str>
<date name="patAssignorEarliestExDate">2019-03-20T04:00:00Z</date>
<arr name="patAssignorName">
<str>CLAMEN, LIANE</str>
<str>GLAZER, PAUL</str>
</arr>
<arr name="patAssigneeName">
<str>OJO, LLC</str>
</arr>
<arr name="applNum">
<str>13096729</str>
</arr>
<arr name="intlRegNum">
<str>NULL</str>
</arr>
<int name="applNumSize">1</int>
<int name="patNumSize">1</int>
<str name="inventionTitleFirst">INJECTABLE PHYSIOLOGICALLY ADAPTIVE INTRAOCULAR LENSES (IOL'S)</str>
<str name="applNumFirst">13096729</str>
<date name="filingDateFirst">2011-04-28T04:00:00Z</date>
<date name="intlPublDateFirst">0001-01-01T00:00:00Z</date>
<str name="intlRegNumFirst">NULL</str>
<date name="issueDateFirst">0001-01-01T00:00:00Z</date>
<str name="patNumFirst">NULL</str>
<date name="publDateFirst">2011-11-03T04:00:00Z</date>
<str name="publNumFirst">20110270389</str>
</doc>
</result>
<lst name="facet_counts">
<lst name="facet_queries">
<int name="conveyanceText:"ASSIGNMENT OF ASSIGNORS INTEREST"">1</int>
<int name="conveyanceText:"SECURITY INTEREST"">0</int>
<int name="conveyanceText:"NUNC PRO TUNC ASSIGNMENT"">0</int>
<int name="conveyanceText:"RELEASE BY SECURED PARTY"">0</int>
<int name="conveyanceText:"MERGER"">0</int>
<int name="conveyanceText:"CHANGE OF NAME"">0</int>
<int name="conveyanceText:"MERGER AND CHANGE OF NAME"">0</int>
<int name="conveyanceText:"LICENSE"">0</int>
<int name="conveyanceText:"LIEN"">0</int>
<int name="conveyanceText:"MORTGAGE"">0</int>
<int name="conveyanceText:"OPTION"">0</int>
<int name="conveyanceText:"DECREE OF DISTRIBUTION"">0</int>
<int name="conveyanceText:"LETTERS OF TESTAMENTARY"">0</int>
<int name="conveyanceText:"LETTERS OF ADMINISTRATION"">0</int>
<int name="conveyanceText:"COURT APPOINTMENT"">0</int>
<int name="conveyanceText:"CONDITIONAL ASSIGNMENT"">0</int>
<int name="conveyanceText:"COURT ORDER"">0</int>
</lst>
<lst name="facet_fields">
<lst name="patAssigneeCityFacet">
<int name="CHESTNUT HILL">1</int>
</lst>
<lst name="patAssigneeStateFacet">
<int name="MASSACHUSETTS">1</int>
</lst>
<lst name="patAssigneePostcodeFacet">
<int name="02467">1</int>
</lst>
<lst name="patAssigneeCountryNameFacet">
<int name="NULL">1</int>
</lst>
<lst name="patAssigneeNameFacet">
<int name="OJO, LLC">1</int>
</lst>
<lst name="corrNameFacet">
<int name="LOCKE LORD LLP">1</int>
</lst>
<lst name="patAssignorNameFacet">
<int name="CLAMEN, LIANE">1</int>
<int name="GLAZER, PAUL">1</int>
</lst>
<lst name="applNum">
<int name="13096729">1</int>
</lst>
<lst name="patNum">
<int name="NULL">1</int>
</lst>
<lst name="publNum">
<int name="20110270389">1</int>
</lst>
<lst name="intlRegNum">
<int name="NULL">1</int>
</lst>
</lst>
</lst>
<lst name="highlighting">
<lst name="48719-909">
<arr name="patAssigneeName">
<str><em>OJO</em>, LLC</str>
</arr>
</lst>
</lst>
</response>
这是我试过的方法:
$client = new Client(array( 'curl' => array( CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST =>0), ));
$res = $client->request('GET', 'https://assignment-api.uspto.gov/patent/basicSearch?query='.$request->input('query'));
$respons = $res->getBody()->getContents();
// dd($respons);
$xml = XmlParser::extract($respons);
$doc = $xml->parse([
'publNum' => ['uses' => 'result.publNum'],
'inventionTitle' => ['uses' => 'result.inventionTitle'],
'inventors' => ['uses' => 'result.inventors'],
'corrName' => ['uses' => 'result.corrName'],
'corrAddress1' => ['uses' => 'result.corrAddress1'],
'corrAddress2' => ['uses' => 'result.corrAddress2'],
'corrAddress3' => ['uses' => 'result.corrAddress3'],
'patAssigneeName' => ['uses' => 'result.patAssigneeName'],
'patAssigneeAddress1' => ['uses' => 'result.patAssigneeAddress1'],
'patAssigneeAddress2' => ['uses' => 'result.patAssigneeAddress2'],
'patAssigneeCity' => ['uses' => 'result.patAssigneeCity'],
'patAssigneeState' => ['uses' => 'result.patAssigneeState'],
'patAssigneeCountryName' => ['uses' => 'result.patAssigneeCountryName'],
]);
dd($doc);
我也尝试过使用 SimpleXml,但似乎都不起作用。请提供任何帮助,我们将不胜感激。谢谢
这可能是因为您使用的选择器是一个属性,而不是实际的元素名称。这与 SimpleXML 等相同,但我在这里所做的是分两步处理它。第一个使用 name
属性作为键和值将其转换为关联数组。如果该值具有子元素,则这些元素本身将作为数组添加...
$xml = simplexml_load_string($respons);
$details = [];
foreach ( $xml->result->doc->children() as $element ) {
if ( count($element) > 0 ) {
foreach ( $element->children() as $sub ) {
$details[(string)$element['name']][] = (string)$sub;
}
}
else {
$details[(string)$element['name']] = (string)$element;
}
}
print_r($details);
将给出
的输出
Array
(
[id] => 48719-909
[reelNo] => 48719
[frameNo] => 909
[conveyanceText] => ASSIGNMENT OF ASSIGNORS INTEREST (SEE DOCUMENT FOR DETAILS).
[corrName] => LOCKE LORD LLP
[corrAddress1] => P.O. BOX 55874
[corrAddress2] => BOSTON, MA 02205
[patAssignorEarliestExDate] => 2019-03-20T04:00:00Z
[patAssignorName] => Array
(
[0] => CLAMEN, LIANE
[1] => GLAZER, PAUL
)
[patAssigneeName] => Array
(
[0] => OJO, LLC
)
[applNum] => Array
(
[0] => 13096729
)
[intlRegNum] => Array
(
[0] => NULL
)
[applNumSize] => 1
[patNumSize] => 1
[inventionTitleFirst] => INJECTABLE PHYSIOLOGICALLY ADAPTIVE INTRAOCULAR LENSES (IOL'S)
[applNumFirst] => 13096729
[filingDateFirst] => 2011-04-28T04:00:00Z
[intlPublDateFirst] => 0001-01-01T00:00:00Z
[intlRegNumFirst] => NULL
[issueDateFirst] => 0001-01-01T00:00:00Z
[patNumFirst] => NULL
[publDateFirst] => 2011-11-03T04:00:00Z
[publNumFirst] => 20110270389
)
(请注意,这只会从 <result>
元素中挑选出值)。
然后您可以使用类似
的方式访问它
'publNum' => $details['publNumFirst'],
'inventionTitle' => $details['inventionTitleFirst'],
这确实假设只有 1 个项目要读取,如果有更多项目,则需要调整 foreach()
并将结果保存为数组的数组(询问您是否遇到困难)。
我一直在尝试使用 laravel 解析来自 USPTO 作业 api 的响应,但一切似乎都是错误的。在此之前,我真的从来没有用 xml 做过这么多。
这是 xml 的示例:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">78</int>
<lst name="params">
<str name="fl">id,reelNo,frameNo,conveyanceText,patAssigneeName,patAssignorName,inventionTitleFirst,applNumFirst,publNumFirst,patNumFirst,intlRegNumFirst,corrName,corrAddress1,corrAddress2,corrAddress3,patAssignorEarliestExDate,filingDateFirst,publDateFirst,issueDateFirst,intlPublDateFirst,patNumSize,applNum,applNumSize,intlRegNum</str>
<str name="sort">patAssignorEarliestExDate desc</str>
<str name="start">0</str>
<str name="q">Ojo</str>
<str name="wt">javabin</str>
<str name="qt">/apisearch</str>
<str name="fq">applNum:(13096729)</str>
<str name="version">2</str>
<str name="rows">8</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="id">48719-909</str>
<str name="reelNo">48719</str>
<str name="frameNo">909</str>
<str name="conveyanceText">ASSIGNMENT OF ASSIGNORS INTEREST (SEE DOCUMENT FOR DETAILS).</str>
<str name="corrName">LOCKE LORD LLP</str>
<str name="corrAddress1">P.O. BOX 55874</str>
<str name="corrAddress2">BOSTON, MA 02205</str>
<date name="patAssignorEarliestExDate">2019-03-20T04:00:00Z</date>
<arr name="patAssignorName">
<str>CLAMEN, LIANE</str>
<str>GLAZER, PAUL</str>
</arr>
<arr name="patAssigneeName">
<str>OJO, LLC</str>
</arr>
<arr name="applNum">
<str>13096729</str>
</arr>
<arr name="intlRegNum">
<str>NULL</str>
</arr>
<int name="applNumSize">1</int>
<int name="patNumSize">1</int>
<str name="inventionTitleFirst">INJECTABLE PHYSIOLOGICALLY ADAPTIVE INTRAOCULAR LENSES (IOL'S)</str>
<str name="applNumFirst">13096729</str>
<date name="filingDateFirst">2011-04-28T04:00:00Z</date>
<date name="intlPublDateFirst">0001-01-01T00:00:00Z</date>
<str name="intlRegNumFirst">NULL</str>
<date name="issueDateFirst">0001-01-01T00:00:00Z</date>
<str name="patNumFirst">NULL</str>
<date name="publDateFirst">2011-11-03T04:00:00Z</date>
<str name="publNumFirst">20110270389</str>
</doc>
</result>
<lst name="facet_counts">
<lst name="facet_queries">
<int name="conveyanceText:"ASSIGNMENT OF ASSIGNORS INTEREST"">1</int>
<int name="conveyanceText:"SECURITY INTEREST"">0</int>
<int name="conveyanceText:"NUNC PRO TUNC ASSIGNMENT"">0</int>
<int name="conveyanceText:"RELEASE BY SECURED PARTY"">0</int>
<int name="conveyanceText:"MERGER"">0</int>
<int name="conveyanceText:"CHANGE OF NAME"">0</int>
<int name="conveyanceText:"MERGER AND CHANGE OF NAME"">0</int>
<int name="conveyanceText:"LICENSE"">0</int>
<int name="conveyanceText:"LIEN"">0</int>
<int name="conveyanceText:"MORTGAGE"">0</int>
<int name="conveyanceText:"OPTION"">0</int>
<int name="conveyanceText:"DECREE OF DISTRIBUTION"">0</int>
<int name="conveyanceText:"LETTERS OF TESTAMENTARY"">0</int>
<int name="conveyanceText:"LETTERS OF ADMINISTRATION"">0</int>
<int name="conveyanceText:"COURT APPOINTMENT"">0</int>
<int name="conveyanceText:"CONDITIONAL ASSIGNMENT"">0</int>
<int name="conveyanceText:"COURT ORDER"">0</int>
</lst>
<lst name="facet_fields">
<lst name="patAssigneeCityFacet">
<int name="CHESTNUT HILL">1</int>
</lst>
<lst name="patAssigneeStateFacet">
<int name="MASSACHUSETTS">1</int>
</lst>
<lst name="patAssigneePostcodeFacet">
<int name="02467">1</int>
</lst>
<lst name="patAssigneeCountryNameFacet">
<int name="NULL">1</int>
</lst>
<lst name="patAssigneeNameFacet">
<int name="OJO, LLC">1</int>
</lst>
<lst name="corrNameFacet">
<int name="LOCKE LORD LLP">1</int>
</lst>
<lst name="patAssignorNameFacet">
<int name="CLAMEN, LIANE">1</int>
<int name="GLAZER, PAUL">1</int>
</lst>
<lst name="applNum">
<int name="13096729">1</int>
</lst>
<lst name="patNum">
<int name="NULL">1</int>
</lst>
<lst name="publNum">
<int name="20110270389">1</int>
</lst>
<lst name="intlRegNum">
<int name="NULL">1</int>
</lst>
</lst>
</lst>
<lst name="highlighting">
<lst name="48719-909">
<arr name="patAssigneeName">
<str><em>OJO</em>, LLC</str>
</arr>
</lst>
</lst>
</response>
这是我试过的方法:
$client = new Client(array( 'curl' => array( CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST =>0), ));
$res = $client->request('GET', 'https://assignment-api.uspto.gov/patent/basicSearch?query='.$request->input('query'));
$respons = $res->getBody()->getContents();
// dd($respons);
$xml = XmlParser::extract($respons);
$doc = $xml->parse([
'publNum' => ['uses' => 'result.publNum'],
'inventionTitle' => ['uses' => 'result.inventionTitle'],
'inventors' => ['uses' => 'result.inventors'],
'corrName' => ['uses' => 'result.corrName'],
'corrAddress1' => ['uses' => 'result.corrAddress1'],
'corrAddress2' => ['uses' => 'result.corrAddress2'],
'corrAddress3' => ['uses' => 'result.corrAddress3'],
'patAssigneeName' => ['uses' => 'result.patAssigneeName'],
'patAssigneeAddress1' => ['uses' => 'result.patAssigneeAddress1'],
'patAssigneeAddress2' => ['uses' => 'result.patAssigneeAddress2'],
'patAssigneeCity' => ['uses' => 'result.patAssigneeCity'],
'patAssigneeState' => ['uses' => 'result.patAssigneeState'],
'patAssigneeCountryName' => ['uses' => 'result.patAssigneeCountryName'],
]);
dd($doc);
我也尝试过使用 SimpleXml,但似乎都不起作用。请提供任何帮助,我们将不胜感激。谢谢
这可能是因为您使用的选择器是一个属性,而不是实际的元素名称。这与 SimpleXML 等相同,但我在这里所做的是分两步处理它。第一个使用 name
属性作为键和值将其转换为关联数组。如果该值具有子元素,则这些元素本身将作为数组添加...
$xml = simplexml_load_string($respons);
$details = [];
foreach ( $xml->result->doc->children() as $element ) {
if ( count($element) > 0 ) {
foreach ( $element->children() as $sub ) {
$details[(string)$element['name']][] = (string)$sub;
}
}
else {
$details[(string)$element['name']] = (string)$element;
}
}
print_r($details);
将给出
的输出Array
(
[id] => 48719-909
[reelNo] => 48719
[frameNo] => 909
[conveyanceText] => ASSIGNMENT OF ASSIGNORS INTEREST (SEE DOCUMENT FOR DETAILS).
[corrName] => LOCKE LORD LLP
[corrAddress1] => P.O. BOX 55874
[corrAddress2] => BOSTON, MA 02205
[patAssignorEarliestExDate] => 2019-03-20T04:00:00Z
[patAssignorName] => Array
(
[0] => CLAMEN, LIANE
[1] => GLAZER, PAUL
)
[patAssigneeName] => Array
(
[0] => OJO, LLC
)
[applNum] => Array
(
[0] => 13096729
)
[intlRegNum] => Array
(
[0] => NULL
)
[applNumSize] => 1
[patNumSize] => 1
[inventionTitleFirst] => INJECTABLE PHYSIOLOGICALLY ADAPTIVE INTRAOCULAR LENSES (IOL'S)
[applNumFirst] => 13096729
[filingDateFirst] => 2011-04-28T04:00:00Z
[intlPublDateFirst] => 0001-01-01T00:00:00Z
[intlRegNumFirst] => NULL
[issueDateFirst] => 0001-01-01T00:00:00Z
[patNumFirst] => NULL
[publDateFirst] => 2011-11-03T04:00:00Z
[publNumFirst] => 20110270389
)
(请注意,这只会从 <result>
元素中挑选出值)。
然后您可以使用类似
的方式访问它'publNum' => $details['publNumFirst'],
'inventionTitle' => $details['inventionTitleFirst'],
这确实假设只有 1 个项目要读取,如果有更多项目,则需要调整 foreach()
并将结果保存为数组的数组(询问您是否遇到困难)。