如何在laravel 5中解析USPTO xml响应

问题描述 投票:1回答:1

我一直在尝试使用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:&quot;ASSIGNMENT OF ASSIGNORS INTEREST&quot;">1</int>
            <int name="conveyanceText:&quot;SECURITY INTEREST&quot;">0</int>
            <int name="conveyanceText:&quot;NUNC PRO TUNC ASSIGNMENT&quot;">0</int>
            <int name="conveyanceText:&quot;RELEASE BY SECURED PARTY&quot;">0</int>
            <int name="conveyanceText:&quot;MERGER&quot;">0</int>
            <int name="conveyanceText:&quot;CHANGE OF NAME&quot;">0</int>
            <int name="conveyanceText:&quot;MERGER AND CHANGE OF NAME&quot;">0</int>
            <int name="conveyanceText:&quot;LICENSE&quot;">0</int>
            <int name="conveyanceText:&quot;LIEN&quot;">0</int>
            <int name="conveyanceText:&quot;MORTGAGE&quot;">0</int>
            <int name="conveyanceText:&quot;OPTION&quot;">0</int>
            <int name="conveyanceText:&quot;DECREE OF DISTRIBUTION&quot;">0</int>
            <int name="conveyanceText:&quot;LETTERS OF TESTAMENTARY&quot;">0</int>
            <int name="conveyanceText:&quot;LETTERS OF ADMINISTRATION&quot;">0</int>
            <int name="conveyanceText:&quot;COURT APPOINTMENT&quot;">0</int>
            <int name="conveyanceText:&quot;CONDITIONAL ASSIGNMENT&quot;">0</int>
            <int name="conveyanceText:&quot;COURT ORDER&quot;">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>&lt;em&gt;OJO&lt;/em&gt;, 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,但似乎一切都没有用。如有任何帮助,将不胜感激。谢谢

php xml laravel xml-parsing nsxmlparser
1个回答
1
投票

这可能是由于您使用的选择器是属性而不是实际的元素名称。这与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'],

这确实假设只有一个项目要读取,如果还有更多,那就是调整foreach()并将结果保持为数组数组(询问是否卡住)。

© www.soinside.com 2019 - 2024. All rights reserved.