0

Im currently working with parsing data from an api-response (xml) but i can't really parse the data. I've previously parsed XML repsonses with xpath, with great success, but in this case it doesnt work.

Here's my code:

    $personnr = $_POST['bcpn'];

    //Get string
    $getstring = 'CustomerLoginName=xxxx&UserLoginName=xxx&Password=xxx&Language=xxx&PersonNumber='.$personnr;
    //Target URL
    $url = "https://xxxxx.se/x/xxx.asmx/DataImport2Person?$getstring";
    //Creating Simple XMl Objects
    $xml = new SimpleXMLElement($url, null, true);

And here is the XML response:

<DataImport2Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.xxx.xxx/xxxx/xxx2">
<Number/>
<Blocks>
<Block>
<Code>PNrStatus</Code>
<Fields>
<Field>
<Code>PNrStatus</Code>
<Value>0</Value>
</Field>
<Field>
<Code>PNrStatusText</Code>
<Value>Registrerat personnummer</Value>
</Field>
</Fields>
</Block>
<Block>
<Code>Fbf</Code>
<Fields>
<Field>
<Code>FinnsIFbf</Code>
<Value>1</Value>
</Field>
<Field>
<Code>FinnsIFbfText</Code>
<Value>Ja</Value>
</Field>
<Field>
<Code>SkyddadMark</Code>
<Value>0</Value>
</Field>
<Field>
<Code>SkyddadMarkText</Code>
<Value>Nej</Value>
</Field>
<Field>
<Code>FbkMark</Code>
<Value>0</Value>
</Field>
<Field>
<Code>FbkMarkText</Code>
<Value/>
</Field>
<Field>
<Code>SparDatum</Code>
<Value>2000-01-01</Value>
</Field>
<Field>
<Code>PNr</Code>
<Value>19590707-1111</Value>
</Field>
<Field>
<Code>FNamn</Code>
<Value>Jan Olof</Value>
</Field>
<Field>
<Code>MNamn</Code>
<Value/>
</Field>
<Field>
<Code>ENamn</Code>
<Value>Håkansson</Value>
</Field>
<Field>
<Code>Coadress</Code>
<Value/>
</Field>
<Field>
<Code>Gatuadress</Code>
<Value>Götgatan</Value>
</Field>
<Field>
<Code>Postnr</Code>
<Value>12639</Value>
</Field>
<Field>
<Code>Postort</Code>
<Value>HÄGERSTEN</Value>
</Field>
<Field>
<Code>TNamn</Code>
<Value>Jan</Value>
</Field>
</Fields>
</Block>
<Block>
<Code>TaxAr1</Code>
<Fields>
<Field>
<Code>TaxAr</Code>
<Value>2010</Value>
</Field>
<Field>
<Code>InkTj</Code>
<Value>211620</Value>
</Field>
<Field>
<Code>InkNarvAkt</Code>
<Value>0</Value>
</Field>
<Field>
<Code>InkNarvPass</Code>
<Value>0</Value>
</Field>
<Field>
<Code>UskNarvAkt</Code>
<Value>0</Value>
</Field>
<Field>
<Code>UskNarvPass</Code>
<Value>0</Value>
</Field>
<Field>
<Code>AllmAvdr</Code>
<Value>5500</Value>
</Field>
<Field>
<Code>TaxForvInk</Code>
<Value>206100</Value>
</Field>
<Field>
<Code>InkKap</Code>
<Value>0</Value>
</Field>
<Field>
<Code>UndKap</Code>
<Value>21908</Value>
</Field>
<Field>
<Code>SamInk</Code>
<Value>184192</Value>
</Field>
<Field>
<Code>SlutSkatt</Code>
<Value>41130</Value>
</Field>
<Field>
<Code>EjFast</Code>
<Value>0</Value>
</Field>
<Field>
<Code>EjFastText</Code>
<Value>Nej</Value>
</Field>
<Field>
<Code>NollTax</Code>
<Value>0</Value>
</Field>
<Field>
<Code>NollTaxText</Code>
<Value>Nej</Value>
</Field>
</Fields>
</Block>
</Blocks>
</DataImport2Result>

As you can see there's alot of "block" which is where the data is located, the ideal Solution for me would be an array where it says <code> => <value>.

I would appreciate any help and suggestions.

All the best, Marten

4
  • 1
    And what's the problem? Why is it that you can't really parse the data? What doesnt work? SimpleXML can use XPath and is iterateable. Sounds like gimme-teh-codez right now. Commented Sep 8, 2011 at 8:35
  • The problem is that xpath wont work, eventhough I am using the "wildcard" which is // Commented Sep 8, 2011 at 8:37
  • 1
    foreach(simplexml_load_file($apiUrl)->Blocks->Block as $block) { Commented Sep 8, 2011 at 8:42
  • possible duplicate of Simple XPath question that drives me crazy and basically any of these stackoverflow.com/search?q=registerXPathNamespace Commented Sep 8, 2011 at 8:53

1 Answer 1

1

You might be missing the namespace. All your elements are in the http://www.xxx.xxx/xxxx/xxx2 namespace, so you have to first issue this command:

$xml->registerXPathNamespace('x', 'http://www.xxx.xxx/xxxx/xxx2');

Then you can address your elements with:

//x:Code
Sign up to request clarification or add additional context in comments.

3 Comments

If that is the issue, the question is a duplicate
Yes, but if you haven’t heard the phrase “XML Namespace” before, it’s hard to search for it.
It's not hard to search for stackoverflow.com/search?q=xpath+not+working+php. It's only more effort to single out the first five results that dont answer the question.

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.