How can I extract data of an external XML to PHP?

How can I extract data from XML to PHP using SimpleXML and echo?

  • I've successfully integrated the LinkedIn API with my website, but I'm struggling to extract information from the XML. At the moment I'm just trying to print it out so I can proceed to use the user's information once they have logged in and given permission. Below is the format of the XML, and further down is the code I am using to extract the information. The "first name", "last name" and "headline" calls work perfectly, but where an element has sub-headings, nothing is printed out. I've tried using echo 'Positions: ' . $xml->{'positions:(title)'}; but it doesn't work. Here is the XML: <person> <id> <first-name /> <last-name /> <headline> <location> <name> <country> <code> </country> </location> <industry> <summary/> <positions total=""> <position> <id> <title> <summary> <start-date> <year> <month> </start-date> <is-current> <company> <name> </company> </position> </person> This is the code I've been using to try to extract the information. I know I have to include the sub-heading somehow but I just don't know how! echo 'First Name: ' . $xml->{'first-name'}; echo '<br/>'; echo 'Last Name: ' . $xml->{'last-name'}; echo '<br/>'; echo 'Headline: ' . $xml->{'headline'}; echo '<br/>'; echo 'Positions: ' . $xml->{'positions'}; Any help would be greatly appreciated, thanks for reading!

  • Answer:

    Using SimpleXML, you'd access the LinkedIn XML data properties as follows: Anything with a dash in the property gets {}, so first-name becomes: $xml->{'first-name'} Anything without a dash such as headline, is referenced like: $xml->headline Anything that is a collection, such as positions, is referenced like: foreach($xml-positions as $position) { echo $position->title; echo $position->{'is-current'}; }

Kate at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Your XML is not valid, its not well formed. Anyway here's a sample XML and how to use it. $v = <<<ABC <vitrine> <canal>Hotwords</canal> <product id="0"> <descricao>MP3 Apple iPod Class...</descricao> <loja>ApetreXo.com</loja> <preco>&#224; vista R$765,22</preco> <urlImagem>http://im</urlImagem> <urlProduto>http://</urlProduto> </product> </vitrine> ABC; $xml = simplexml_load_string($v); foreach ($xml->product as $c){ echo $c->loja; //echoing out value of 'loja' }

Mob

Try to use PHP's XML Parser instead: http://www.php.net/manual/en/function.xml-parse.php

mOrSa

Your XML is not well-formed... there are several elements without close tags. So we have no way to know for sure the structure of your XML. (You can't do that in XML like you can in HTML.) That being said, assuming that <person> is the context node, you can probably get the content of the <title> element using an XPath expression, as in $xml->xpath('positions/position/title'); I'm assuming $xml is a SimpleXMLElement object.

LarsH

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.