我真的需要在包含无效XML值的字段中提供有关字符串解析的帮助。 我将显示当前值与目标值放在字符串字段中。
我有一个带有此值的字段$ xmlString(元素不在SEPERATE行中但在SAME行中;它是Web服务响应,所以我只对以后的解析没有影响响应):
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv=" http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p:queryBillingAccountResponse xmlns:p="http://www.ibm.com">
<ns0:customerAccount xmlns:ns0=" http://www.ibm.com/2009">
<ComponentCustomerAccount>
<Name>ADSL 4</Name>
<CharacteristicValue>
<Characteristic>
<Name>Balance</Name>
</Characteristic>
<Value>0.0</Value>
</CharacteristicValue>
<AccountStatus>Paid</AccountStatus>
</ComponentCustomerAccount>
</ns0:customerAccount>
</p:queryBillingAccountResponse>
</soapenv:Body>
</soapenv:Envelope>
如果可能的话,我想要这个输出:
<queryBillingAccountResponse>
<customerAccount>
<ComponentCustomerAccount>
<Name>ADSL 4</Name>
<CharacteristicValue>
<Characteristic>
<Name>Balance</Name>
</Characteristic>
<Value>0.0</Value>
</CharacteristicValue>
<CharacteristicValue>
<AccountStatus>Paid</AccountStatus>
</ComponentCustomerAccount>
</customerAccount>
</queryBillingAccountResponse>
所以你会注意到我没有前三行(虽然它们不是真正的单独行)和最后两行,但我没有为queryBilling AccountResponse
和customer Account
定义名称空间。我希望没有名称空间的这些元素在字符串字段中。对于开始和结束标记。我真的需要这个输出。如何解析这个?我尝试使用SimpleXMLElement但无法解析它。
谢谢你的帮助
$ xml = simplexml_load_string($ text)无法解析的更新输出;
<<<XML
<?xml version="1.0" encoding="utf-8"?>
<Envelope>
<Body>
<queryBillingAccountResponse>
<customerAccount>
<ComponentCustomerAccount>
<Name>ADSL 4</Name>
<CharacteristicValue>
<Characteristic>
<Name>Balance</Name>
</Characteristic>
<Value>0.0</Value>
</CharacteristicValue>
<AccountStatus>Paid</AccountStatus>
</ComponentCustomerAccount>
</customerAccount>
</queryBillingAccountResponse>
</Body>
</Envelope>
XML>
答案 0 :(得分:1)
为了获得SimpleXML可以理解的xml代码,并且由于您不需要命名空间声明,以下代码在将代码应用于simplexml_load_string
之前清除代码
<?php
// if the XML comes from a file (or just assign the $text string)
$text = file_get_contents('myfile.xml');
$text = preg_replace('/(<\s*)\w+:/','$1',$text); // removes <xxx:
$text = preg_replace('/(<\/\s*)\w+:/','$1',$text); // removes </xxx:
$text = preg_replace('/\s+xmlns:[^>]+/','',$text); // removes xmlns:...
// the code should be clean enough for SimpleXML to parse it
$xml = simplexml_load_string($text);
// view the XML (and process it afterwards...)
print_r($xml);
将示例XML放在字符串(而不是文件)中
<?php
$text = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv=" http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p:queryBillingAccountResponse xmlns:p="http://www.ibm.com">
<ns0:customerAccount xmlns:ns0=" http://www.ibm.com/2009">
<ComponentCustomerAccount>
<Name>ADSL 4</Name>
<CharacteristicValue>
<Characteristic>
<Name>Balance</Name>
</Characteristic>
<Value>0.0</Value>
</CharacteristicValue>
<AccountStatus>Paid</AccountStatus>
</ComponentCustomerAccount>
</ns0:customerAccount>
</p:queryBillingAccountResponse>
</soapenv:Body>
</soapenv:Envelope>
XML;
$text = preg_replace('/(<\s*)\w+:/','$1',$text); // removes <xxx:
$text = preg_replace('/(<\/\s*)\w+:/','$1',$text); // removes </xxx:
$text = preg_replace('/\s+xmlns:[^>]+/','',$text); // removes xmlns:...
// the code should be clean enough for SimpleXML to parse it
$xml = simplexml_load_string($text);
// view the XML (and process it afterwards...)
print_r($xml);
要访问元素,请使用->
(和数组[xx]
),例如
echo echo $xml->Body->queryBillingAccountResponse->customerAccount->ComponentCustomerAccount->Name . "\n";
将显示
ADSL 4