Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 简单XML删除标记之间的字符串_Php_Xml - Fatal编程技术网

Php 简单XML删除标记之间的字符串

Php 简单XML删除标记之间的字符串,php,xml,Php,Xml,我有一个包含XML的产品。我需要从这个XML中获取:描述、价格、重量等。 当我使用SimpleXML打开XML并打印时,标记之间没有文本。 这是我的XML的一部分 <product id="30" currency="PLN" code_producer="M-1100/21"> <producer id="1282294004" name="MAT lingerie" /> <category id="1214553882" xml:lang="p

我有一个包含XML的产品。我需要从这个XML中获取:描述、价格、重量等。 当我使用SimpleXML打开XML并打印时,标记之间没有文本。 这是我的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>
如何使用SimpleXML读取标记之间的文本并保存到对象?

XML末尾缺少标记“
”。可能是在c&p中丢失的

不管怎样,我使用的测试代码都是完美的,对我来说,这是读取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;
}

不适合我,我得到[attributes]=>Array([xml:lang]=>eng)没有描述。我不会错过我发布的小xml代码,我的代码xml文件很大。正如您所看到的,我的描述值包含其他节点,这些节点带有“value”和文本,如“Sofia biustonosz półusztywniany M-1100/21”,您正在寻找它,对吗?是的,但我的值为空。我将声明数组从$array=[]更改;到$array=array();该代码基于5.4。如果您没有得到任何解析错误,那么您使用的是5.4,因此array()或[]不会产生任何更改。你能复制我的代码并在你的盒子里运行吗?你不应该只删除“
<?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;
}