1

我真的需要关于字段中包含无效 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 AccountResponseand定义命名空间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>
4

1 回答 1

2

为了有一个 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 文档

于 2013-03-23T13:02:36.707 回答