从损坏的XML文件中获取特定的XML部分

时间:2011-02-25 12:03:21

标签: php xml substring

我有一个XML文件,我从外部服务器(通过卷曲)获取,但由于某种原因,它已损坏,SimpleXML疯狂,每次我尝试解析时都会发出错误,即使它是在一个字符串中。所以,我决定尝试获取我需要的XML文件的特定部分:

<codes>
    <code1code>There is no Code available.</code1code>
    <code2text>Please try again later.</code2text>
</codes>

我该怎么做?我试过substr,preg_match,str_replace,我到处搜索,但没有一个解决方案有效!请帮帮我,谢谢!

1 个答案:

答案 0 :(得分:1)

Yoy的意思是那样的

$info = <<<text
<test>
<codes>
        <code1code>There is no Code available.</code1code>
        <code2text>Please try again later.</code2text>
    </codes>
text;


    $xml = new SimpleXMLElement ($info, LIBXML_NOERROR|LIBXML_ERR_NONE|LIBXML_ERR_FATAL);

请注意,test标记未关闭,但会阻止任何错误。

object(SimpleXMLElement)#1 (1) {
  ["codes"]=>
  object(SimpleXMLElement)#2 (2) {
    ["code1code"]=>
    string(27) "There is no Code available."
    ["code2text"]=>
    string(23) "Please try again later."
  }
}