PHP-如何解析字符串字段(删除一些XML元素和所有名称空间)?

PHP-如何解析字符串字段(删除一些XML元素和所有名称空间)?,php,xml,string,Php,Xml,String,在包含无效XML值的字段中,我确实需要有关字符串解析的帮助。 我将在字符串字段中显示当前值和目标值 我有一个带此值的字段$xmlString(元素不在单独的行中,而是在同一行中;它是web服务响应,因此我仅在以后解析时对响应没有影响): 为了获得SimpleXML可以理解的xml代码,并且由于您不需要名称空间声明,下面的代码在将代码应用于SimpleXML\u load\u string <?php // if the XML comes from a file (or just

在包含无效XML值的字段中,我确实需要有关字符串解析的帮助。 我将在字符串字段中显示当前值和目标值

我有一个带此值的字段$xmlString(元素不在单独的行中,而是在同一行中;它是web服务响应,因此我仅在以后解析时对响应没有影响):


为了获得SimpleXML可以理解的xml代码,并且由于您不需要名称空间声明,下面的代码在将代码应用于
SimpleXML\u load\u 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);
将显示

ADSL 4

这与其他有什么不同?为什么我不应该将此标记为重复?请不要,因为我不知道如何删除名称空间。这是完全不同的。谢谢你的理解
<<<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>
<?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);
    <?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);
    echo echo $xml->Body->queryBillingAccountResponse->customerAccount->ComponentCustomerAccount->Name . "\n";
ADSL 4