0

I have a XML doc , with 3 article nodes in it , each Article contains : Title Image Link

I need to be able to get the Title/Image/Link values out of the node.

$query = $xpath->query('//section/article');

foreach($query as $currentArticle => $artContents):
print_r( $artContents->title);
endforeach

; This doesn't work for me, I can use ->NodeName and nodeValue , but they dont drill down enough , just display 'article' or the whole contents respectively.

To explain more:

My XML is

<article>
<title>Title </title>
<link>http:// </link>
<img>image src </img>
</article>

and the output I require is :

Title->Title

Link->http://

etc.

Update to explain :

foreach($query as $articles):
foreach($articles->childNodes as $childNode) {

    if ($childNode->nodeType === XML_ELEMENT_NODE) {

                  $stored =    $childNode->nodeValue;

                    array_push( $availAds, $stored );           

    }
}

endforeach;

is what I currently have thanks to Gordon . This though makes my array look like:

//previous values:

      }
  ["1300884672_071.jpg"]=>
  array(3) {
    ["image"]=>
    string(30) "1300884672_071.jpg"
    ["title"]=>
    string(6) "secind title"
    ["link"]=>
    string(10) "grtgrtgrtg"
  }
  ["1300884618_071.jpg"]=>
  array(3) {
    ["image"]=>
    string(30) "1300884618_071.jpg"
    ["title"]=>
    string(5) "first title"
    ["link"]=>
    string(10) "http://www.google.com"
  }

//updated values that 
  [0]=>
  string(6) "My Title"
  [1]=>
  string(89) "/1300961550.jpg"
  [2]=>
  string(22) "rtherherhgerg thursada"
  [3]=>
  string(20) "custome 222222222222"

I obviously need my array to be consistent, but cannot work out how to do that. Thanks for your patience.

Bob

0

1 Answer 1

1

I'm still not sure what the problem is, but are you looking for

foreach($query as $articles):
    foreach($articles->childNodes as $childNode) {
        if ($childNode->nodeType === XML_ELEMENT_NODE) {
            printf("%s->%s%s", $childNode->nodeName, $childNode->nodeValue, PHP_EOL);
        }
    }
}

This will iterate over all the DOMElement nodes that are direct children of the $article nodes returned in the query and print them in this format "nodename->nodevalue" and a newline.

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

1 Comment

Hi thanks, this is sort of what I'm trying to achieve, sorry for my poor explanation. This will output the values , but I need to be able to reference each value to place them into an array. So Im trying to get : $title = $childNode->title $img = $childNode->img etc. which I can then place into the correct array. Thanks. Ive sorted it with a if statement.

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.