Fetch specific tag with an attribute value from XM

2019-01-29 14:50发布

问题:

I have a XML file like the following:

<?xml version="1.0" encoding="UTF-8"?>
<response uri="/crm/private/xml/SalesOrders/getRecords">
    <result>
        <SalesOrders>
            <row no="1">
                <FL val="SALESORDERID">580005000000187001</FL>
                <FL val="SO Number"><![CDATA[580005000000187002]]></FL>
                <FL val="Subject"><![CDATA[Sales order test from sri 02]]></FL>
                <FL val="Due Date"><![CDATA[2013-03-10]]></FL>
                <FL val="Status"><![CDATA[Created]]></FL>
                <FL val="ACCOUNTID">580005000000088096</FL>
                <FL val="Account Name"><![CDATA[Best Western Pavilions]]></FL>
                <FL val="SMOWNERID">580005000000052003</FL>
                <FL val="Sales Order Owner"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="SMCREATORID">580005000000052003</FL>
                <FL val="Created By"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="MODIFIEDBY">580005000000052003</FL>
                <FL val="Modified By"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="Created Time"><![CDATA[2013-01-17 20:32:19]]></FL>
                <FL val="Modified Time"><![CDATA[2013-01-17 20:32:19]]></FL>
                <FL val="Sub Total"><![CDATA[250]]></FL>
                <FL val="Tax"><![CDATA[0]]></FL>
                <FL val="Adjustment"><![CDATA[0]]></FL>
                <FL val="Grand Total"><![CDATA[250]]></FL>
                <FL val="Product Details">
                    <product no="1">
                        <FL val="Product Id">580005000000171125</FL>
                        <FL val="Product Name"><![CDATA[The Grilling Maestro]]></FL>
                        <FL val="Unit Price">10.0</FL>
                        <FL val="Quantity">20.0</FL>
                        <FL val="Quantity in Stock">0.0</FL>
                        <FL val="Total">200.0</FL>
                        <FL val="Discount">0.0</FL>
                        <FL val="Total After Discount">200.0</FL>
                        <FL val="List Price">10.0</FL>
                        <FL val="Net Total">200.0</FL>
                        <FL val="Tax">0.0</FL>
                        <FL val="Product Description"><![CDATA[null]]></FL>
                    </product>
                    <product no="2">
                        <FL val="Product Id">580005000000171131</FL>
                        <FL val="Product Name"><![CDATA[Uptown Deli   Soup du Jour]]></FL>
                        <FL val="Unit Price">25.0</FL>
                        <FL val="Quantity">1.0</FL>
                        <FL val="Quantity in Stock">0.0</FL>
                        <FL val="Total">50.0</FL>
                        <FL val="Discount">1.23</FL>
                        <FL val="Total After Discount">23.77</FL>
                        <FL val="List Price">25.0</FL>
                        <FL val="Net Total">48.77</FL>
                        <FL val="Tax">0.0</FL>
                        <FL val="Product Description"><![CDATA[null]]></FL>
                    </product>
                </FL>
                <FL val="Terms and Conditions"><![CDATA[iBanquet T and C]]></FL>
                <FL val="Description"><![CDATA[Sales Order from iBanquet]]></FL>
                <FL val="Discount"><![CDATA[0]]></FL>
            </row>
            <row no="2">
                <FL val="SALESORDERID">580005000000134001</FL>
                <FL val="SO Number"><![CDATA[580005000000134003]]></FL>
                <FL val="Subject"><![CDATA[Test]]></FL>
                <FL val="CONTACTID">580005000000120009</FL>
                <FL val="Contact Name"><![CDATA[Sydney Lai]]></FL>
                <FL val="Carrier"><![CDATA[FedEX]]></FL>
                <FL val="Excise Duty"><![CDATA[0]]></FL>
                <FL val="Sales Commission"><![CDATA[0]]></FL>
                <FL val="Status"><![CDATA[Created]]></FL>
                <FL val="ACCOUNTID">580005000000088100</FL>
                <FL val="Account Name"><![CDATA[Best Western Sportstown Anaheim]]></FL>
                <FL val="SMOWNERID">580005000000052003</FL>
                <FL val="Sales Order Owner"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="SMCREATORID">580005000000052003</FL>
                <FL val="Created By"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="MODIFIEDBY">580005000000052003</FL>
                <FL val="Modified By"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="Created Time"><![CDATA[2012-11-08 14:51:50]]></FL>
                <FL val="Modified Time"><![CDATA[2012-11-08 14:51:50]]></FL>
                <FL val="Sub Total"><![CDATA[45]]></FL>
                <FL val="Tax"><![CDATA[0]]></FL>
                <FL val="Adjustment"><![CDATA[0]]></FL>
                <FL val="Grand Total"><![CDATA[45]]></FL>
                <FL val="Product Details">
                    <product no="1">
                        <FL val="Product Id">580005000000059016</FL>
                        <FL val="Product Name"><![CDATA[Beef Stoganoff]]></FL>
                        <FL val="Unit Price">14.95</FL>
                        <FL val="Quantity">1.0</FL>
                        <FL val="Quantity in Stock">-25.0</FL>
                        <FL val="Total">45.0</FL>
                        <FL val="Discount">0.0</FL>
                        <FL val="Total After Discount">45.0</FL>
                        <FL val="List Price">45.0</FL>
                        <FL val="Net Total">45.0</FL>
                        <FL val="Tax">0.0</FL>
                        <FL val="Product Description"><![CDATA[null]]></FL>
                    </product>
                </FL>
                <FL val="Discount"><![CDATA[0]]></FL>
            </row>
            <row no="3">
                <FL val="SALESORDERID">580005000000118001</FL>
                <FL val="SO Number"><![CDATA[580005000000118003]]></FL>
                <FL val="Subject"><![CDATA[SAirpriya's Birthday]]></FL>
                <FL val="POTENTIALID">580005000000069001</FL>
                <FL val="Potential Name"><![CDATA[Sandeep]]></FL>
                <FL val="Due Date"><![CDATA[2012-10-17]]></FL>
                <FL val="Carrier"><![CDATA[FedEX]]></FL>
                <FL val="Excise Duty"><![CDATA[0]]></FL>
                <FL val="Sales Commission"><![CDATA[0]]></FL>
                <FL val="Status"><![CDATA[Created]]></FL>
                <FL val="ACCOUNTID">580005000000088090</FL>
                <FL val="Account Name"><![CDATA[Best Western Anaheim Inn]]></FL>
                <FL val="SMOWNERID">580005000000052003</FL>
                <FL val="Sales Order Owner"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="SMCREATORID">580005000000052003</FL>
                <FL val="Created By"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="MODIFIEDBY">580005000000052003</FL>
                <FL val="Modified By"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="Created Time"><![CDATA[2012-10-16 14:45:18]]></FL>
                <FL val="Modified Time"><![CDATA[2012-10-16 14:45:18]]></FL>
                <FL val="Sub Total"><![CDATA[1080]]></FL>
                <FL val="Tax"><![CDATA[0]]></FL>
                <FL val="Adjustment"><![CDATA[0]]></FL>
                <FL val="Grand Total"><![CDATA[1080]]></FL>
                <FL val="Product Details">
                    <product no="1">
                        <FL val="Product Id">580005000000059016</FL>
                        <FL val="Product Name"><![CDATA[Beef Stoganoff]]></FL>
                        <FL val="Unit Price">14.95</FL>
                        <FL val="Quantity">24.0</FL>
                        <FL val="Quantity in Stock">-25.0</FL>
                        <FL val="Total">1080.0</FL>
                        <FL val="Discount">0.0</FL>
                        <FL val="Total After Discount">1080.0</FL>
                        <FL val="List Price">45.0</FL>
                        <FL val="Net Total">1080.0</FL>
                        <FL val="Tax">0.0</FL>
                        <FL val="Product Description"><![CDATA[null]]></FL>
                    </product>
                </FL>
                <FL val="Discount"><![CDATA[0]]></FL>
            </row>
            <row no="4">
                <FL val="SALESORDERID">580005000000070009</FL>
                <FL val="SO Number"><![CDATA[580005000000070011]]></FL>
                <FL val="Subject"><![CDATA[iBanquet]]></FL>
                <FL val="QUOTEID">580005000000070003</FL>
                <FL val="Quote Name"><![CDATA[iBanquet]]></FL>
                <FL val="Carrier"><![CDATA[UPS]]></FL>
                <FL val="Excise Duty"><![CDATA[0]]></FL>
                <FL val="Sales Commission"><![CDATA[0]]></FL>
                <FL val="Status"><![CDATA[Created]]></FL>
                <FL val="ACCOUNTID">580005000000058001</FL>
                <FL val="Account Name"><![CDATA[Hotel Skysurf]]></FL>
                <FL val="SMOWNERID">580005000000052003</FL>
                <FL val="Sales Order Owner"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="SMCREATORID">580005000000052003</FL>
                <FL val="Created By"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="MODIFIEDBY">580005000000052003</FL>
                <FL val="Modified By"><![CDATA[Adithya Buddhavarapu]]></FL>
                <FL val="Created Time"><![CDATA[2012-06-19 19:17:12]]></FL>
                <FL val="Modified Time"><![CDATA[2012-06-29 19:55:00]]></FL>
                <FL val="Sub Total"><![CDATA[20538]]></FL>
                <FL val="Tax"><![CDATA[0]]></FL>
                <FL val="Adjustment"><![CDATA[0]]></FL>
                <FL val="Grand Total"><![CDATA[20538]]></FL>
                <FL val="Product Details">
                    <product no="1">
                        <FL val="Product Id">580005000000059032</FL>
                        <FL val="Product Name"><![CDATA[Carrot Cake]]></FL>
                        <FL val="Unit Price">10.0</FL>
                        <FL val="Quantity">441.0</FL>
                        <FL val="Quantity in Stock">-882.0</FL>
                        <FL val="Total">4410.0</FL>
                        <FL val="Discount">0.0</FL>
                        <FL val="Total After Discount">4410.0</FL>
                        <FL val="List Price">10.0</FL>
                        <FL val="Net Total">4410.0</FL>
                        <FL val="Tax">0.0</FL>
                        <FL val="Product Description"><![CDATA[null]]></FL>
                    </product>
                    <product no="2">
                        <FL val="Product Id">580005000000059022</FL>
                        <FL val="Product Name"><![CDATA[Chicken Parmigiano]]></FL>
                        <FL val="Unit Price">15.0</FL>
                        <FL val="Quantity">122.0</FL>
                        <FL val="Quantity in Stock">-122.0</FL>
                        <FL val="Total">1464.0</FL>
                        <FL val="Discount">0.0</FL>
                        <FL val="Total After Discount">1464.0</FL>
                        <FL val="List Price">12.0</FL>
                        <FL val="Net Total">1464.0</FL>
                        <FL val="Tax">0.0</FL>
                        <FL val="Product Description"><![CDATA[null]]></FL>
                    </product>
                    <product no="3">
                        <FL val="Product Id">580005000000059030</FL>
                        <FL val="Product Name"><![CDATA[Coconut Cake]]></FL>
                        <FL val="Unit Price">10.0</FL>
                        <FL val="Quantity">1222.0</FL>
                        <FL val="Quantity in Stock">-1222.0</FL>
                        <FL val="Total">14664.0</FL>
                        <FL val="Discount">0.0</FL>
                        <FL val="Total After Discount">14664.0</FL>
                        <FL val="List Price">12.0</FL>
                        <FL val="Net Total">14664.0</FL>
                        <FL val="Tax">0.0</FL>
                        <FL val="Product Description"><![CDATA[null]]></FL>
                    </product>
                </FL>
                <FL val="Discount"><![CDATA[0]]></FL>
            </row>
        </SalesOrders>
    </result>
</response>

I need to show only the name of the sales order owner name, which are storing under <FL val="Sales Order Owner"> tag.

After getting the xml, I convert into an array and then try to access. My code is like:

foreach ($xml_sales->result->SalesOrders->row as $t) {
  foreach ($t->FL as $rr) {
    echo $rr."<br />";  
  }
}

Which is showing all FL values.

/*********After editing********/

$file_sales_content = "https://crm.zoho.com/crm/private/xml/SalesOrders/getRecords?authtoken=XXX&scope=crmapi"  
$doc = new DOMDocument();
$doc->loadXML( $file_sales_content );   
$xpath = new DOMXPath( $doc );
$items = $xpath->evaluate( '//FL[@val="Sales Order Owner"]' );
for ( $i = 0; $i < $items->length; $i++ ) {
 echo $items->item( $i )->textContent . '<br>';
}

Actually by this url, I am getting the xml file. But finally I got some warning like: Warning: DOMDocument::loadXML() [domdocument.loadxml]: Start tag expected, '<' not found in Entity, line: 1 in C:\xampp\htdocs\ibanquet\manager\user_list.php on line 34

回答1:

All you need to do is just create DOMDocument object, load your xml, create DOMXpath object for your document and evaluate xpath expression:

$xml = file_get_contents( "https://crm.zoho.com/crm/private/xml/SalesOrders/getRecords?authtoken=XXX&scope=crmapi" );

$doc = new DOMDocument();
$doc->loadXML( $xml );

$xpath = new DOMXPath( $doc );
$items = $xpath->evaluate( '//FL[@val="Sales Order Owner"]' );
for ( $i = 0; $i < $items->length; $i++ ) {
    echo $items->item( $i )->textContent . '<br>';
}

It will output:

Adithya Buddhavarapu
Adithya Buddhavarapu
Adithya Buddhavarapu
Adithya Buddhavarapu


回答2:

You best express that with an Xpath expression:

$owner = ($nodes = $xml_sales->xpath('//FL[@val = "Sales Order Owner"][1]')) 
         ? (string) $nodes[0] 
         : NULL;

This will give:

Adithya Buddhavarapu

in your case. If the element is not found it is NULL.

//FL[@val = "Sales Order Owner"][1]

This is the xpath expression. It reads:

Take any FL element of the document that has the val attribute with the value "Sales Order Owner" that is the first element ([1]).

If you need that per row, it works similar, just run the xpath per row. See SimpleXMLElement::xpath.