Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
5 replies Latest Post - ‏2011-04-30T16:38:57Z by SystemAdmin
DIM3NSION
DIM3NSION
3 Posts
ACCEPTED ANSWER

Pinned topic Trouble linking in XML document [XPath]

‏2011-04-25T22:32:28Z |
Hi Guys, i have an external xml document that features a lot of content that i don't want, currently all the information in that document is located within <title> tags. I thought xPath would be the best way to pull this data into another PHP page.

the code i am using -

<?php

$xml = simplexml_load_file('rss.xml');

/* Search for <a><b><c> */
$result = $xml->xpath('/title');

while(list( , $node) = each($result)) {
echo '/title: ',$node,"\n";
}
?>

the file is linked correctly, however it isn't echoing out anything :/ im stuck....any pointers?

Thanks
Updated on 2011-04-30T16:38:57Z at 2011-04-30T16:38:57Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    746 Posts
    ACCEPTED ANSWER

    Re: Trouble linking in XML document [XPath]

    ‏2011-04-26T02:08:30Z  in response to DIM3NSION
    "title" is not the root element of any rss. Do this to locate them all.
    
    $result = $xml->xpath(
    '//title');
    
    • DIM3NSION
      DIM3NSION
      3 Posts
      ACCEPTED ANSWER

      Re: Trouble linking in XML document [XPath]

      ‏2011-04-26T23:14:51Z  in response to SystemAdmin
      Perfect tsuji. I want to manipulate this data, so that every result is loaded into an array? is that possible. So all the titles in the xml all have there own array number?
      • SystemAdmin
        SystemAdmin
        746 Posts
        ACCEPTED ANSWER

        Re: Trouble linking in XML document [XPath]

        ‏2011-04-27T13:10:47Z  in response to DIM3NSION
        The return of the xpath ($result) is already an array or boolean false in case of finding none. Nevertheless it is an array of SimpleXMLElement objects.

        If you mean an array of the text values alone rather than the element objects, you can do it like this (with $a holding the desired text values).
        
        $a=array(); 
        
        while(list( , $node) = each($result)) 
        { echo 
        '/title: ',$node,
        "\n"; $a[]=(string)$node; 
        }
        
        • DIM3NSION
          DIM3NSION
          3 Posts
          ACCEPTED ANSWER

          Re: Trouble linking in XML document [XPath]

          ‏2011-04-30T14:42:22Z  in response to SystemAdmin
          I have that working, instead of me posting a new topic i have another issue. Basically im using cURl to send a response to this api. I then load the response into a simpleXMLelement.

          The output of that file looks like this -

          [2] => SimpleXMLElement Object
          (
          @attributes => Array
          (
          num => 3
          type => artist
          )

          title => DJ Tiësto
          uri => http://www.discogs.com/artist/DJ+Ti%C3%ABsto
          summary => DJ Tiësto Tijs Michiel Verwest Dutch trance DJ & producer

          But obviously theres more results like albums and tracks he has done in the array element

          How would I go about selecting all the data and just displaying the track releases - Ive tried this code in php but with no luck

          $xmlmusic = new SimpleXMLElement($result, NULL, false);

          foreach ($xmlmusic as $xm)
          {
          $attrs = $xm->attributes();
          if($attrs == "title")
          echo $xm->summary."\n";
          }
          Any Help would be much appreciated

          Thanks
          • SystemAdmin
            SystemAdmin
            746 Posts
            ACCEPTED ANSWER

            Re: Trouble linking in XML document [XPath]

            ‏2011-04-30T16:38:57Z  in response to DIM3NSION
            I have no idea what is the question as of the abstraction/exposition done. When you summarize a situation, every effort should be vested to make it clear rather than make it a riddle. In this case, what is the matching xpath to warrant the "title" now appearing as a child? Should it be trying to match "item" or whatever? If yes, why it is not mentioned, as it is something rather disconnected with the original question?

            >How would I go about selecting all the data and just displaying the track releases
            What is a track? what is a track release? What is "selecting all the data"? What is "just displaying the track releases"?

            >if($attrs == "title")
            What title now have anything to do with attribute? disregarding whether the above make a sense or not?

            I can't help with that. Somebody else may and is encouraged to help.