Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select according to ITEM_ID all values in XML array

I need to select one whole array according to the internal value of ITEM_ID.

XML:

<SHOP>
    <SHOPITEM>
        <ITEM_ID>459878</ITEM_ID>
        <PRODUCTNAME>Lenovo Tab P11 (2nd Gen) LTE, 4/128GB, storm grey + originálna klávesnica (SK layout) v hodnote 99€</PRODUCTNAME>
        <PRODUCT>Lenovo Tab P11 (2nd Gen) LTE, 4/128GB, storm grey + originálna klávesnica (SK layout) v hodnote 99€</PRODUCT>
    </SHOPITEM>


    <SHOPITEM>
        <ITEM_ID>461986</ITEM_ID>
        <PRODUCTNAME>Microsoft Surface Go 3, 6500Y, 4GB, 64GB LTE, Platinový</PRODUCTNAME>
        <PRODUCT>Microsoft Surface Go 3, 6500Y, 4GB, 64GB LTE, Platinový</PRODUCT>
    </SHOPITEM>
</SHOP>

my code written so far:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$xml = new SimpleXMLElement("http://URL.eu/xml.xml", null, true);
$xpath = $xml->xpath(expression: "//SHOP/SHOPITEM/ITEM_ID[.='459878']");

#print_r($xpath);

foreach ($xpath as $data)
{
    echo 'id: '.$data[0], PHP_EOL;
}

I need to select all the data only according to "ITEM_ID" into the array

this is what i need to achieve:

$ITEM_DATA = array(
    'ITEM_ID' => 145123,
    'PRODUCTNAME' => 'example',
    'PRODUCT' => 'example'
);

such as selecting from a database where "select * from Table WHERE ITEM_ID = 141312"

thanks for the advices, and excuse my english.

>Solution :

Your xpath expression selects the actual node with the text content 459878 – what you’re looking for is to select the SHOPITEM node that contains that node, which you can do with

/SHOP/SHOPITEM[ITEM_ID[.=459878]]

The square brackets specify an xpath predicate, similar to the where clause you mention in your question:

"Get me the SHOPITEM node that has a child ITEM_ID node that contains the text 459787"

The other small modification is to change the // to /, which will perform a little better since it doesn’t have to search the entire document. If your actual document is more complex than your example then you might need to adjust that.

Full example:

$xpath = $xml->xpath(expression: "/SHOP/SHOPITEM[ITEM_ID[.='459878']]");

var_dump((array) $xpath[0]);

Results:

array(3) {
  ["ITEM_ID"]=>
  string(6) "459878"
  ["PRODUCTNAME"]=>
  string(103) "Lenovo Tab P11 (2nd Gen) LTE, 4/128GB, storm grey + originálna klávesnica (SK layout) v hodnote 99€"
  ["PRODUCT"]=>
  string(103) "Lenovo Tab P11 (2nd Gen) LTE, 4/128GB, storm grey + originálna klávesnica (SK layout) v hodnote 99€"
}

See https://3v4l.org/55KGh

Edit to add a quick note that casting a SimpleXMLElement to an array isn’t always the best way to access children – you can lose data, or end up including data that you don’t want. Just using it here to show the results, the best option will depend on your full document structure.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading