0

I'm trying to parse a "not-so-normal" XML web service. It appears the web service is returning a string, with XML inside that string.

The XML looks like this:

<string>
  <NewDataSet>
    <Table>
      <CATID>0</CATID>
      <DEPID>0</DEPID>
    </Table>
    <Table>
      <CATID>1</CATID>
      <DEPID>1</DEPID>
    </Table>
  </NewDataSet>
<string>

This is my code as of right now, I've already tried numerous variations but can't get anything to work. Most of the time it just returns nothing. I've echoed $xml and it will return all the XML, but I just need to show the CATID and nothing else.

<?php

$file = file_get_contents('http://theWebSericeLink.here');
$xml = simplexml_load_string($file);

 //echo $xml;
 foreach($xml->Table as $item) {
   echo $item->CATID;
 }
?>

I've also tried:

   foreach($xml->NewDataSet->Table as $item)

and

   foreach($xml->String->NewDataSet->Table as $item)

Thanks in advance.

4
  • last closing tag (string) is written with mistake (without /). Does this mistake exist in source? Commented Aug 30, 2013 at 14:01
  • @Will Irvine Are you check my answer Commented Aug 30, 2013 at 14:26
  • Sorry, that was my typo. The source does have the closing tag. Commented Aug 30, 2013 at 17:42
  • BTW simplexml_load_file would load that XML file (URL) directly btw. Commented Aug 31, 2013 at 22:56

4 Answers 4

1

Try this

<?php
$k = '<string>
    <NewDataSet>
        <Table>
          <CATID>0</CATID>
          <DEPID>0</DEPID>
        </Table>
        <Table>
          <CATID>1</CATID>
          <DEPID>0</DEPID>
        </Table>
    </NewDataSet>
</string>';
$xml = simplexml_load_string($k);

   foreach($xml->NewDataSet->Table as $read){       
     echo $read->CATID."<br>";
    }
?>

Output

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

1 Comment

This does work in your example, but using the return from my web service it would not. I'm not sure why, but it just wouldn't work no matter how I tried. The problem here was the web service I had to use, not you guys. :) I did figure it out though, check out my answer.
0

If you can see the XML when you echo it, then you may need to use xpath.

$id = $xml->xpath('/table/CATID'); 

1 Comment

I tried this and couldn't get it to work. I did figure it out though, check out my answer.
0

As a first step, close the tag at the end, and start with "print_r" the xml variable.

print_r($xml);

As there are parents and children tags, It is like an array to get a result through each field.

In your case,

<string>
   <NewDataSet>
     <Table>

So

$tables = $xml->NewDataSet->Table;

foreach ($tables as $table)
{
    foreach ($table->CATID as $catid){
        echo $catid, "\n";
    }
}

2 Comments

Dont parse string. Error occurred. Notice: Trying to get property of non-object in on line 16
I tried this and couldn't get it to work. I did figure it out though, check out my answer.
0

I figured it out.

Before my foreach function, I created an xml file from the "xml string" the web service was returning:

$xml = simplexml_load_string($file);
$feed = '<?xml version="1.0" encoding="ISO-8859-1"?>'.$xml;

file_put_contents("workhorse.xml", $feed);
$myxml=simplexml_load_file("workhorse.xml");

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.