0
votes

I have to parse a xml data I am using simplexml following is the simple xml object i got

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 1.2
        )

    [file] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [source-language] => en
                    [datatype] => plaintext
                    [original] => file.ext
                )

            [body] => SimpleXMLElement Object
                (
                    [trans-unit] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 1
                                        )

                                    [source] => search_term
                                    [target] => %count% result for "%query%" has been found.|%count% results for "%query%" have been found.
                                    [alt-trans] => SimpleXMLElement Object
                                        (
                                            [target] => Hola mundo
                                        )

                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 2
                                        )

                                    [source] => search_noresults
                                    [target] => Sorry, I found no results for your query on the website.
                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 3
                                        )

                                    [source] => search_failure
                                    [target] => We are very sorry, but the search service is not available at the moment.
                                )

                            [3] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 4
                                        )

                                    [source] => search
                                    [target] => Search
                                )

                            [4] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 5
                                        )

                                    [source] => previous
                                    [target] => Prev
                                )

                            [5] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 6
                                        )

                                    [source] => next
                                    [target] => Next
                                )

                            [6] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 7
                                        )

                                    [source] => Search this site
                                    [target] => Search this site
                                )

                            [7] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 8
                                        )

                                    [source] => send
                                    [target] => Send
                                )

                        )

                )

        )

) 

Problem arise when i try to parse the data by following code

$this->glossary = new SimpleXMLElement($this->text);
foreach($this->glossary->file->body->trans-unit as $transunit)
{
  //parsing code
}

i get a php error as variable cannot have "-" in between and we have trans-unit as a object variable.We cannot change xml file as it is in the predefined format.

I have another question regarding simplexml i have the following xml unit

    <trans-unit id="1">
        <source>search_term</source>
        <target>%count% result for &quot;%query%&quot; has been found.|%count% results for &quot;%query%&quot; have been found.</target>
        <alt-trans>
        <target xml:lang='es'>Hola mundo</target>
        </alt-trans>
    </trans-unit>

i want to extract the "lang='es'" namespace from <alt-trans> tag in the foreach loop the defined above.

1
Please have a look at the Basic SimpleXML usage section of the manual, esp. the example #3. - Álvaro González
oh my god how can i miss that thanks btw can you answer my second question also - Ryan decosta

1 Answers

0
votes

2nd question:

retrieve xml:lang with xpath:

$xml = simplexml_load_string($x); // assume XML in $x

$target = (string)$xml->xpath("//target[@xml:lang]")[0];
$lang = (string)$xml->xpath("//target[@xml:lang]/@xml:lang")[0];

note: requires PHP >= 5.4, if your PHP version is below, let me know there's a workaround.

see it in action: https://eval.in/98015