1

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!

2
  • Please correct the XML so it's well-formed, or give a link to the erroneous source. Also it's unclear what you mean by sub-headings. Do you mean child elements? Commented Nov 8, 2011 at 15:53
  • I fixed my answer based on mlitn's help. If you haven't got anything working yet, try it. Commented Nov 8, 2011 at 16:04

5 Answers 5

3

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'};
}
Sign up to request clarification or add additional context in comments.

Comments

1

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'
}

1 Comment

Thanks, actually I copied the XML from the LinkedIn website, I didn't write it. I'll give this a go!
0

Try to use PHP's XML Parser instead:

http://www.php.net/manual/en/function.xml-parse.php

Comments

0

Tried Paul's answer above for:

foreach($xml-positions as $position) {
    echo $position->title;
    echo $position->{'is-current'};
}   

didn't work for me - so I used this - not as elegant but works

    for($position_num = 0; $position_num < 10;$position_num++){
        echo $xml->positions->position[$position_num]->company->name;
    }

Comments

-1

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.

4 Comments

Thanks a lot, I took this XML from the LinkedIn website (I didn't write it!), it's the XML which is provided when you request information from the API. I tried your suggestion and it didn't work :( thanks though!
positions/position/title looks more like xpath - be.php.net/manual/en/simplexmlelement.xpath.php $xml->xpath('positions/position/title');
@Kate: what you showed is not XML, even if someone said it was. Can you post a link to the page where you found it?
@mlitn: yes, since no information was given about the nature of $xml, I assumed it was an XPath processor. I didn't check. That sort of information should come from the asker, hence my implied question at the end.

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.