0

I'm new to PHP. I'm trying to get the data out of the below XML. Now, in my code $data->Address contains value of the below code i.e:

$data->Address = "<tolist></tolist>
        <cclist>
            <cc>
                <contactpersonname>niraj</contactpersonname>
                <name>niraj</name>
                <email>stgh@gmail.com</email>
                <number>+91.3212365212</number>
                <prefix>Ms.</prefix>
                <contactpersonprefix>Ms.</contactpersonprefix>
            </cc>
            <cc>
                <contactpersonname>fdg</contactpersonname>
                <name>admin</name>
                <email>admin12@gmail.com</email>
                <number>+91.4554343234</number>
                <prefix>Mr.</prefix>
                <contactpersonprefix>Mr.</contactpersonprefix>
            </cc>
        </cclist>";

Now I want to extract the <contactpersonname> tag and print it. How can I do this?

George Brighton
  • 5,131
  • 9
  • 27
  • 36
user3004356
  • 870
  • 4
  • 16
  • 49

4 Answers4

1

You can convert the XML to an array with the following code:

$xml   = simplexml_load_string($buffer);
$array = json_decode(json_encode((array) $xml), 1);

Where $buffer is the xml string.

Then you can obtain the person name as follow:

$data->Address = $array['cclist']['cc']['contactpersonname'];

It's a quick and dirty method to convert the xml to an array, but it works.

Pakspul
  • 648
  • 5
  • 17
  • It is quick. It is not dirty. Using the built in parser is the right way to do this. Now the question was edited you might want to expand your answer to include how to handle an array of values. – Floris Jan 17 '14 at 23:37
  • It is giving me an empty $array. `$array=Array ( [0] => )` – user3004356 Jan 17 '14 at 23:39
  • This is because you have two root nodes, tolist and cclist. Can you fit them in a metadata node, thus {...}. Then the simplexml_load_string would not give a error. – Pakspul Jan 17 '14 at 23:41
  • Can I concatenate and add it in the first and last line. Please guide me – user3004356 Jan 17 '14 at 23:43
  • I don't know if you can change the XML data, if so you can add an placeholder metadata (because it's all about metadata of email i presume?), this means your XML data will look like {...} – Pakspul Jan 17 '14 at 23:47
  • Yes...I can change the data. It is only displaying it – user3004356 Jan 17 '14 at 23:50
  • Now the json_decode is displaying this-`Array ( [tolist] => Array ( ) [cclist] => Array ( [cc] => Array ( [0] => Array ( [contactpersonname] => niraj [name] => niraj [email] => stgh@gmail.com [number] => +91.3212365212 [prefix] => Ms. [contactpersonprefix] => Ms. ) [1] => Array ( [contactpersonname] => fdg [name] => admin [email] => admin12@gmail.com [number] => +91.4554343234 [prefix] => Mr. [contactpersonprefix] => Mr. ) ) ) [from] => Array ( [name] => niraj [email] => niraj@groupz.com ) )` – user3004356 Jan 17 '14 at 23:53
  • You should be able to use the following, otherwise play with it. This is basic PHP. $data->Address = $array['cclist']['cc'][0]['contactpersonname']; – Pakspul Jan 17 '14 at 23:55
  • I get this warning - `Undefined offset: 0 ` – user3004356 Jan 17 '14 at 23:58
1

Since your XML is missing a tag that encompasses all others, you need to create on in order to get parsers to work properly:

<?php
$buffer = "<tolist></tolist>
        <cclist>
            <cc>
                <contactpersonname>niraj</contactpersonname>
                <name>niraj</name>
                <email>stgh@gmail.com</email>
                <number>+91.3212365212</number>
                <prefix>Ms.</prefix>
                <contactpersonprefix>Ms.</contactpersonprefix>
            </cc>
            <cc>
                <contactpersonname>fdg</contactpersonname>
                <name>admin</name>
                <email>admin12@gmail.com</email>
                <number>+91.4554343234</number>
                <prefix>Mr.</prefix>
                <contactpersonprefix>Mr.</contactpersonprefix>
            </cc>
        </cclist>";

//   ***** wrap the whole thing in a <root> tag...
$xml   = simplexml_load_string("<root>".$buffer."</root>");
$array = json_decode(json_encode((array) $xml), 1);

echo "<pre>";
print_r($array);
echo "</pre>";
?>

Result:

Array
(
    [tolist] => Array
        (
        )

    [cclist] => Array
        (
            [cc] => Array
                (
                    [0] => Array
                        (
                            [contactpersonname] => niraj
                            [name] => niraj
                            [email] => stgh@gmail.com
                            [number] => +91.3212365212
                            [prefix] => Ms.
                            [contactpersonprefix] => Ms.
                        )

                    [1] => Array
                        (
                            [contactpersonname] => fdg
                            [name] => admin
                            [email] => admin12@gmail.com
                            [number] => +91.4554343234
                            [prefix] => Mr.
                            [contactpersonprefix] => Mr.
                        )

                )

        )

)

UPDATED

Now you can navigate down to where you want to go with

echo "<pre>";
$ccList = $array['cclist'];
$cc = $ccList['cc'];
$contacts = array();
foreach($cc as $i=>$val) {
  $contacts[$i]=$val['contactpersonname'];
}
echo "first contact: " . $contacts[0] . "<br>";
echo "second contact: " . $contacts[1] ."<br>";

Result:

first contact: niraj
second contact: fdg
Floris
  • 45,857
  • 6
  • 70
  • 122
  • What you have posted works fine till displaying the array. When I try the later part to display the contactpersonname. I get this warning - `Illegal string offset 'contactpersonname' ` – user3004356 Jan 18 '14 at 00:10
  • I updated my code to show how to access the array elements, but I suspect you have already figured it out since you just accepted the answer. – Floris Jan 18 '14 at 00:34
0

Try this..

$xml = new SimpleXMLElement($string);
$results = $xml->xpath('cclist/cc/contactpersonname');

http://php.net/manual/en/simplexmlelement.xpath.php

Mamuz
  • 1,730
  • 12
  • 14
  • I get this warning 'Extra content at the end of the document ' – user3004356 Jan 17 '14 at 23:41
  • The xml string is not well formed. You have to specify a root element . http://stackoverflow.com/questions/16972737/xml-parse-error-extra-content-at-the-end-of-the-document – Mamuz Jan 17 '14 at 23:43
0
$xml = simplexml_load_file("note.xml");
echo $xml->contactpersonname;

This requires you to load it form an xml file. If you already have the string in the code I'd recommend a regex. If you know the data won't ever be incorrect written!

$pattern = '#<contactpersonname>(.*?)</contactpersonname>#';
echo preg_match ($pattern, $data->Address);
Emz
  • 1,280
  • 1
  • 14
  • 29