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

时间:2019-04-30 09:51:29

标签: php xml laravel xml-parsing nsxmlparser

我一直在尝试使用laravel解析来自USPTO分配api的响应,而一切似乎都错了。在此之前,我真的从未对xml做太多事情。

以下是xml示例:

5cad672e902bb3438441bd9c

这是我尝试过的:

   <?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>

我也尝试过使用SimpleXml,但是似乎都无法正常工作。请任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分: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()并将结果保留为数组数组(如果被卡住,请询问)。