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


PHP - how to parse STRING field (remove some XML elements and all namespaces)?

我真的需要一个关于包含无效XML值的字段中的字符串解析的帮助。我将显示当前值和要放在字符串字段中的目标值。

我有一个字段$xmlString与此值(元素不是在单独的行,但在同一行;这是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 AccountResponsecustomer 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>

为了拥有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

SimpleXML医生