0

I have XML contains product. I need take from this XML: description, price, weight etc. When I opening XML using SimpleXML and printing i don't have text between tags. This is piece of my XML

<product id="30" currency="PLN" code_producer="M-1100/21">
    <producer id="1282294004" name="MAT lingerie" />
    <category id="1214553882" xml:lang="pol" name="BIUSTONOSZE/Półusztywniane" />
    <unit id="0" xml:lang="pol" name="szt." />
    <series id="4" xml:lang="pol" name="Basic Collection" />
    <card url="http://b2b.tiffanie.pl/product-pol-30-Sofia-biustonosz-polusztywniany-M-1100-21.html" />
    <description>
        <name xml:lang="eng">Sofia biustonosz półusztywniany M-1100/21</name>
        <name xml:lang="pol">Sofia biustonosz półusztywniany M-1100/21</name>
        <version name="czarny">
            <name xml:lang="eng">Sofia biustonosz półusztywniany M-1100/21</name>
            <name xml:lang="pol">czarny</name>
        </version>
        <long_desc xml:lang="eng">
            <span style="font-family: Tahoma, Geneva, sans-serif; color: #3f3229;">Delikatny biustonosz dla kobiet które cenią sobie komfort i wygodę. Biustonosz typu soft o perfekcyjnej konstrukcji pozwala zebrać biust jednocześnie doskonale, utrzymać go na miejscu. Miseczki zdobione są subtelnym kwiatowym haftem. Model posiada regulowane, odpinane ramiączka.</span>
        </long_desc>
        <long_desc xml:lang="pol">
            <span style="font-family: Tahoma, Geneva, sans-serif; color: #3f3229;">Delikatny biustonosz dla kobiet które cenią sobie komfort i wygodę. Biustonosz typu soft o perfekcyjnej konstrukcji pozwala zebrać biust jednocześnie doskonale, utrzymać go na miejscu. Miseczki zdobione są subtelnym kwiatowym haftem. Model posiada regulowane, odpinane ramiączka.</span>
        </long_desc>
    </description>

Result in print_r

[description] => SimpleXMLElement Object
(
    [name] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [lang] => eng
                        )
                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [lang] => pol
                        )
                )
        )

    [long_desc] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [lang] => eng
                        )
                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [lang] => pol
                        )
                )
        )
)

How to use SimpleXML to read text between tags and save to object?

1 Answer 1

1

You have a missing tag "</product>" at the end of the XML. Maybe was missing in the c&p

Any way this my test code using XMLReader works perfect and for me is the easiest way of read XML.

<?php

$input = file_get_contents('http://set4812.pl/web/1.xml');
$input = str_replace(['<![CDATA[', ']]>'], '', $input);

$reader = new XMLReader();
$reader->xml($input);
$r = xmlToArray($reader);
print_r($r);

function xmlToArray(XMLReader $xml){ 
    $array = []; 

    $i = 0; 
    while($xml->read()){ 
        if ($xml->nodeType == XMLReader::END_ELEMENT) {
            break; 
        } 

        if ($xml->nodeType == XMLReader::ELEMENT && !$xml->isEmptyElement) { 
            unset($array[$i]);
            $array[$i]['name'] = $xml->name; 
            $array[$i]['values'] = xmlToArray($xml); 
            $array[$i]['attributes'] = getAttributesFromNode($xml);
            $i++; 
        } else if ($xml->isEmptyElement) { 
            $array[$i]['name'] = $xml->name; 
            $array[$i]['values'] = null; 
            $array[$i]['attributes'] = getAttributesFromNode($xml);
            $i++;
        } else if($xml->nodeType == XMLReader::TEXT) {
            $array[$i]['values'] = $xml->value; 
        }
    }

    return $array; 
}

function getAttributesFromNode(XMLReader $xml)
{
    if(!$xml->hasAttributes) {
        return null;
    }

    $attributes = [];
    while($xml->moveToNextAttribute()) {
        $attributes[$xml->name] = $xml->value; 
    }
    
    return $attributes;
}
Sign up to request clarification or add additional context in comments.

10 Comments

Not working for me i getting [attributes] => Array ( [xml:lang] => eng ) There is no description. I not miss </product> i post small xml code, my code xml file is big.
As you can see my description values contains other nodes with "values" with text like "Sofia biustonosz półusztywniany M-1100/21" you are looking for that right?
Yes but my value is empty. I change declaration array from $array = []; to $array=array();
The code is based on 5.4. If you dont get any parse error you are on 5.4 so array() or [] not produce any changes. Can you copy my code and run in your box?
You should not just remove the "<![CDATA[", this can break the XML. The problem is, that you only handle XMLReader::ELEMENT and XMLReader::TEXT, you need to handle XMLReader::CDATA, too.
|

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.