2

so i have this XML: the result of var_dump

string '
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data>
<entry key="message Type">
<![
    CDATA[
        urgent
    ]
]></entry><entry key="message Title"><![
    CDATA[
        teswt
    ]
]></entry><entry key="message Body"><![
    CDATA[
        teasd
    ]
]></entry><entry key="message Priority"><![
    CDATA[
        1
    ]
]></entry></data>
' (length=260)

my code looks like this:

$xml = simplexml_load_string($tablerow['data']);
$inputs = $xml->xpath("/schema/user_input[@type!='hidden']/input|/schema/input");
foreach($inputs as $cur_input) {

        $cur_input_name = (string)$cur_input['name'];
        $cur_input_value = $task_input_values[$cur_input_name];
        $input_name = isset($cur_input['label']) ? (string)$cur_input['label'] : $cur_input['name'];
}
        var_dump($tablerow['data']);

$tablerow['data'] is the XML from the DB. any help how to get the message Title value from this??

Thanks!

2 Answers 2

2

The XPath expression you are using doesn't even resemble the XML document you posted, so I don't know how you would expect to ever find the values you are looking for. The following will extract "teswt" from your sample XML document:

<?php
$xml = simplexml_load_string($tablerow['data']);
$title = '';

foreach ($xml->entry as $entry) {
    if ((string) $entry['key'] == 'message Title') {
        $title = (string) $entry;
        break;
    }
}

echo $title;
?>

Edit: Updated to use the SimpleXML "style" rather than XPath.

Sign up to request clarification or add additional context in comments.

2 Comments

it's working but if i have 3 messages and the last two are empty it will show $title as "teswtteswtteswt"...
The code in my answer as it is currently written will only result in the text from one element being saved to $title. It's possible you copied it incorrectly.
0

Your XML should be in proper way.

Check This

    <?php
    $xmlString = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <data>
                <entry key="message Type">
                    <![CDATA[urgent]]>
                </entry>
                <entry key="message Title">
                    <![CDATA[teswt]]>
                </entry>
                <entry key="message Body">
                    <![CDATA[xxxxx]]>
                </entry>
                <entry key="message Priority">
                    <![CDATA[1]]>
                </entry>
            </data>';

    $xml = simplexml_load_string($xmlString);

    foreach ($xml->entry as $entry)
    {   
       echo $entry['key']."<br/>";
    }
    ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.