Veavictis
Veavictis

Reputation: 15

Attribute value from xml

I use the following code to read an xml file

$xml = file_get_contents('file.xml');<br>
include('xml_lib.php');<br>
$data = XML_unserialize($xml);<br>
$i = 0;<br>
foreach ($data[ordersetChangeReport][tradeItem] as $value) {<br>
    $productNumber = $data[ordersetChangeReport][tradeItem][$i][sfgItemNumber];<br>
    $gewichtProduct = $data[ordersetChangeReport][tradeItem][$i][sfgTotalNetVolume];<br>
    $metadata = array(<br>
        'gewichtProduct' => $gewichtProduct,<br>
    );<br>
    foreach ($metadata as $meta_key => $metavalue) {<br>
        update_post_meta($productNumber, $meta_key, $metavalue);<br>
    }<br>
    $i++;<br>
}

My question: How can I get the attrubute value "unitOfMeasure" from the field "sfgTotalNetVolume"?

Many thanks in advance.

Upvotes: 0

Views: 47

Answers (3)

Veavictis
Veavictis

Reputation: 15

Big thanks guys for the quick & informative answers.

The rule I needed to know was exactly:

$xml->tradeItem->sfgTotalNetVolume->attributes()->unitOfMeasure;

It works like a charm.

Upvotes: 0

ThW
ThW

Reputation: 19492

You're using some library that converts the XML into an array in a generic way. That is a bad idea usually because it means loosing information and depending directly on the input XML structure.

It is not that difficult to use the DOM extension directly with Xpath:

$xml = <<<'XML'
<ordersetChangeReport> <tradeItem> <sfgItemNumber>196306</sfgItemNumber> <sfgTotalNetVolume unitOfMeasure="LT">1.00000</sfgTotalNetVolume> </tradeItem> <tradeItem> <sfgItemNumber>674892</sfgItemNumber> <sfgTotalNetVolume unitOfMeasure="KG">1.50000</sfgTotalNetVolume> </tradeItem> </ordersetChangeReport>
XML;

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

foreach ($xpath->evaluate('/ordersetChangeReport/tradeItem') as $item) {
  var_dump(
    [
       'number' => $xpath->evaluate('string(sfgItemNumber)', $item),
       'volume' => [
         'value'=> $xpath->evaluate('number(sfgTotalNetVolume)', $item),
         'unit'=> $xpath->evaluate('string(sfgTotalNetVolume/@unitOfMeasure)', $item)
       ]
    ]
  );
}

Output:

array(2) {
  ["number"]=>
  string(6) "196306"
  ["volume"]=>
  array(2) {
    ["value"]=>
    float(1)
    ["unit"]=>
    string(2) "LT"
  }
}
array(2) {
  ["number"]=>
  string(6) "674892"
  ["volume"]=>
  array(2) {
    ["value"]=>
    float(1.5)
    ["unit"]=>
    string(2) "KG"
  }
}

Upvotes: 1

Tanuel Mategi
Tanuel Mategi

Reputation: 1283

Okay, now this is a little complex since you are using a library and a xml file i don't know. however, i came up with a little solution using the built in SimpleXML class.

This is a basic example how to access the attributes, you will need to code some iteration ;)

here we go:

<?php
$xml_string = '<ordersetChangeReport> <tradeItem> <sfgItemNumber>196306</sfgItemNumber> <sfgTotalNetVolume unitOfMeasure="LT">1.00000</sfgTotalNetVolume> </tradeItem> <tradeItem> <sfgItemNumber>674892</sfgItemNumber> <sfgTotalNetVolume unitOfMeasure="KG">1.50000</sfgTotalNetVolume> </tradeItem> </ordersetChangeReport>';

$xml = simplexml_load_string($xml_string);

$unitOfMeasure =  $xml->tradeItem->sfgTotalNetVolume->attributes()->unitOfMeasure;

echo $unitOfMeasure;
?>

use print_r($xml); to view the whole tree. Keep in mind that not all attributes will be displayed, so you need to use print_r on the children, like

print_r($xml->tradeItem->sfgTotalNetVolume);

EDIT: Little info:

the variable $unitOfMeasure is NOT a string-type variable, but a SimpleXMLElement Object. you can still access it like a string since it implements the toString()-method

Upvotes: 0

Related Questions