PHP: Parsing XML file

Hi
I'm attempting to pass an XML file

    $xml = simplexml_load_file('test.xml');
 
   var_dump($xml);

Open in new window


Produces

\\Path\To\test.php
object(SimpleXMLElement)[1]
  public 'title' => string "My Title" (length=8)
 ........
public  'properties ' =>
           array (size=1234)
                  0=>
                    object(SimpleXMLElement)[2]
                  2=>
                    object(SimpleXMLElement)[2]
                  3=>
                    object(SimpleXMLElement)[4]

Open in new window


Before some one asks I cannot upload the xml file but it looks like this (There might be errors due to fat fingers)

<?xml version="1.0" encoding="UTF-8" ?>
<resultset  xmlns="http://example.com/" xmlns:ns2="http://example.com/path/to/file" xmlns:ns2="http://example.com/path/to/other/file">
  <title>The Searchers</title>
  <date>1956</date>
  <properties >
    <ns2:name>overview</ns2:name>
    <ns2:value  xmlns:xs="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">As a Civil War veteran spends years searching for a young niece captured by Indians, his motivation becomes increasingly questionable.</ns2:value>
  </properties >
  <properties >
    <ns2:name>Director</ns2:name>
    <ns2:value  xmlns:xs="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">John Ford</ns2:value>
  </properties >
  <properties >
    <ns2:name>actors</ns2:name>
    <ns2:value xmlns:xsi="http://example.com/XMLSchema" xsi:type="ns2:list">
       <ns2:entries xmlns:xs="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">John Wayne</ns2:entries>
       <ns2:entries xmlns:xs="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">Jeffrey Hunter</ns2:entries>
       <ns2:entries xmlns:xs="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">Vera Miles</ns2:entries>
       <ns2:entries xmlns:xs="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">Natalie Wood</ns2:entries>
</ns2:value>
  </properties >
 <filmData>
    <location>Monument Valley</location>
     <imdb>https://www.imdb.com/title/tt0049730/</imdb>
 <filmData>
</resultset>';

Open in new window


Could someone suggest how to get the value of  title and how to loop through the "Properties Array"  accessing the elements of   object(SimpleXMLElement)[2] (In the XML these are Name Value pears that don't show up in the var_dump also the parts of filmData?
LVL 1
trevor1940Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
The title
$title = (string)$xml->title;

Open in new window

The properties. But there is a watch-it here - the third entry has an array of entries so you would need to account for that in your output

foreach($xml->properties as $r) {
  echo "Name: " . $r->name . "<br>";
  echo "Value:" . $r->value . "<br>";
}

Open in new window

0
trevor1940Author Commented:
The title works but

foreach($xml->properties as $r) {
  echo "Name: " . $r->name . "<br>";
  echo "Value:" . $r->value . "<br>";
}

Open in new window


Gives
Name:
Value:
Name:
Value:

Open in new window


I'm assuming I can get above to work I'll need do something like

foreach($xml->properties as $r) {
  $name = $r->name;
  if($name == "actors"){
    // process as array??
}
else{
  echo "Name: " . $name . "<br>";
  echo "Value:" . $r->value . "<br>";
  }
}

Open in new window

0
Julian HansenCommented:
It would depend on your data - I had to work on a modified version of the xml you posted as it did not work as presented.

You can also try casting your values to strings (string)$r->name.

As for accessing the entries - you can just check if it exists and then do
$xml = simplexml_load_file('t3400.xml');
$title = $xml->title;
echo $title;
echo "<pre>";
foreach($xml->properties as $r) {
	echo "Name: " . $r->name . "\n";

	if (isset($r->value->entries)) {
		foreach($r->value->entries as $p) {
			echo "A: {$p}\n";
		}
	}
	else {
		echo "Value:" . $r->value . "\n";
	}
}
echo "</pre>";

Open in new window

Using XML
<?xml version="1.0" encoding="UTF-8" ?>
<resultset  xmlns:xs="http://example.com/" xmlns:ns2="http://example.com/path/to/other/file">
  <title>The Searchers</title>
  <date>1956</date>
  <properties>
    <name>overview</name>
    <value>As a Civil War veteran spends years searching for a young niece captured by Indians, his motivation becomes increasingly questionable.</value>
  </properties>
  <properties >
    <name>Director</name>
    <value>John Ford</value>
  </properties >
  <properties>
    <name>actors</name>
    <value>
       <entries xmlns:xsi="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">John Wayne</entries>
       <entries xmlns:xsi="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">Jeffrey Hunter</entries>
       <entries xmlns:xsi="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">Vera Miles</entries>
       <entries xmlns:xsi="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">Natalie Wood</entries>
</value>
  </properties >
 <filmData>
    <location>Monument Valley</location>
     <imdb>https://www.imdb.com/title/tt0049730/</imdb>
 </filmData>
</resultset>

Open in new window

Produces
The Searchers

Name: overview
Value:As a Civil War veteran spends years searching for a young niece captured by Indians, his motivation becomes increasingly questionable.
Name: Director
Value:John Ford
Name: actors
A: John Wayne
A: Jeffrey Hunter
A: Vera Miles
A: Natalie Wood

Open in new window

0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

trevor1940Author Commented:
I wish I was able to show  you a real example but It's just not possible
 
$name =  (string)$r->name;

Open in new window


Still doesn't reveal anything

Doing something like A perl script i've  written in the past just errors
 
$name =  (string)$r->ns2:name; 

Open in new window



This isn't the way to do it I'm sure but by reading the XML in using file_get_contents
Then use str_replace to remove "ns2:"
Then process the string with $xml = simplexml_load_string($xml);

I can then access the name value pares  as shown
0
Julian HansenCommented:
What does this produce

echo "<pre>" . print_r($xml, true) . "</pre>";

Open in new window


Can we at least see what the dump looks like to see how data is stored internally.
0
Julian HansenCommented:
What about this. In this version we get the namespaces using getNamespaces and then use this result to find the items we need in the XML. I had to fix up a few typo's in the XML to get your version to work - but this should produce the correct result.
<?php
$xml = simplexml_load_file('t3400.xml');
$ns = $xml->getNamespaces(true);
$title = $xml->title;
echo "<pre>";

foreach($xml->properties as $r) {
  echo "Name: " . $r->children($ns['ns2'])->name . "\n";

  if (isset($r->children($ns['ns2'])->value->entries)) {
    foreach($r->children($ns['ns2'])->value->entries as $p) {
      echo "A: {$p}\n";
    }
  }
  else {
    echo "Value:" . $r->children($ns['ns2'])->value . "\n";
  }
}
echo "</pre>";

Open in new window

Updated XML
<?xml version="1.0" encoding="UTF-8" ?>
<resultset  xmlns="http://example.com/" xmlns:ns2="http://example.com/path/to/other/file">
  <title>The Searchers</title>
  <date>1956</date>
  <properties >
    <ns2:name>overview</ns2:name>
    <ns2:value  xmlns:xsi="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">As a Civil War veteran spends years searching for a young niece captured by Indians, his motivation becomes increasingly questionable.</ns2:value>
  </properties >
  <properties >
    <ns2:name>Director</ns2:name>
    <ns2:value  xmlns:xsi="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">John Ford</ns2:value>
  </properties >
  <properties >
    <ns2:name>actors</ns2:name>
    <ns2:value xmlns:xsi="http://example.com/XMLSchema" xsi:type="ns2:list">
       <ns2:entries xmlns:xs="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">John Wayne</ns2:entries>
       <ns2:entries xmlns:xs="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">Jeffrey Hunter</ns2:entries>
       <ns2:entries xmlns:xs="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">Vera Miles</ns2:entries>
       <ns2:entries xmlns:xs="http://www.w3.org/2001/XMLSchema"  xsi:type="xs:string">Natalie Wood</ns2:entries>
	</ns2:value>
  </properties >
 <filmData>
    <location>Monument Valley</location>
     <imdb>https://www.imdb.com/title/tt0049730/</imdb>
 </filmData>  
</resultset>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris Harte2015 Top Expert (Most Article Points)Commented:
0
trevor1940Author Commented:
Hi
Not sure about last comment

Will try when I'm back in the office
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.