简体   繁体   中英

How can I extract value from an xml

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 ie:

$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?

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.

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

Try this..

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

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

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM