3

I have following xml structure:

<stores>
   <store>
      <name></name>
      <address></address>
      <custom-attributes>
          <custom-attribute attribute-id="country">Deutschland</custom-attribute>
          <custom-attribute attribute-id="displayWeb">false</custom-attribute>
      </custom-attributes>
   </store>
</stores>

how can i get the value of "displayWeb"?

1
  • there are no criterias, i just want to get the value "false" Commented Feb 6, 2012 at 14:55

4 Answers 4

2

The best solution for this is use PHP DOM, you may either loop trough all stores:

$dom = new DOMDocument();
$dom->loadXML( $yourXML);

// With use of child elements:
$storeNodes = $dom->documentElement->childNodes;

// Or xpath
$xPath = new DOMXPath( $dom);
$storeNodes = $xPath->query( 'store/store');

// Store nodes now contain DOMElements which are equivalent to this array:
// 0 => <store><name></name>....</store>
// 1 => <store><name>Another store not shown in your XML</name>....</store>

Those uses DOMDocument properties and DOMElement attribute childNodes or DOMXPath. Once you have all stores you may iterate trough them with foreach loop and get either all elements and store them into associative array with getElementsByTagName:

foreach( $storeNodes as $node){
  // $node should be DOMElement
  // of course you can use xPath instead of getAttributesbyTagName, but this is
  // more effective
  $domAttrs = $node->getAttributesByTagName( 'custom-attribute');
  $attributes = array();
  foreach( $domAttrs as $domAttr){
    $attributes[ $domAttr->getAttribute( 'attribute-id')] = $domAttr->nodeValue;
  }
  // $attributes = array( 'country' => 'Deutschland', 'displayWeb' => 'false');
}

Or select attribute directly with xPath:

// Inside foreach($storeNodes as $node) loop 
$yourAttribute = $xPath->query( "custom-attribute[@attribute-id='displayWeb']", $node)
     ->item(0)->nodeValue; // Warning will cause fatal error when missing desired tag

Or when you need just one value from whole document you could use (as Kirill Polishchuk suggested):

$yourAttribute = $xPath->query( "stores/store/custom-attributes/custom-attribute[@attribute-id='displayWeb']")
    ->item(0)->nodeValue; // Warning will cause fatal error when missing desired tag

Carefully study manual to understand what type is returned when and what does which attribute contain.

Sign up to request clarification or add additional context in comments.

Comments

1

For example I can parse XML DOM. http://php.net/manual/en/book.dom.php

1 Comment

do you also have an concrete example in this case?
1

You can use XPath:

stores/store/custom-attributes/custom-attribute[@attribute-id='displayWeb']

Comments

1

I'd suggest PHP's SimpleXML. That web page has lots of user-supplied examples of use to extract values from the parsed data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.