1

I really need a help about string parsing in field containg not valid XML value. I will display current value with target value to put in string field.

I have a field $xmlString with this value (elements are NOT in the SEPERATE lines but in the SAME line; it is web service response so I do not have impact on response only on later parsing):

<?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>

I want this output if this is possible:

<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>

So you will notice that I do not have first three lines (although they are not really seperate lines) and last two lines and I do not have namespaces defined for queryBilling AccountResponse and customer Account. I want these elements without namespace to be in string field. For both on start and end tag. I really need this output. How to parse this? I tried with SimpleXMLElement but could not parse it. Thank you for your help

Updated output which can not be parsed by $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>
2
  • How is this different from your other question? Why should I not mark this as a duplicate? Commented Mar 23, 2013 at 12:42
  • please dont because I do not know how to remove namespaces. It is completely different. thank you for understanding Commented Mar 23, 2013 at 13:04

1 Answer 1

2

In order to have a xml code that SimpleXML can understand, and since you don't need the namespaces declarations, the following code cleans the code before to apply it to 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);

To put the sample XML in a string (instead of a file)

    <?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);

To access elements, use -> (and [xx] for arrays), e.g

    echo echo $xml->Body->queryBillingAccountResponse->customerAccount->ComponentCustomerAccount->Name . "\n";

will display

ADSL 4

SimpleXML doc

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.