Jump to content

How to foreach through an XML file and store bullet points with similar names


cb2004
 Share

Recommended Posts

Hi all, I think I have been looking at this too long and I am sure it is so simple. I am reading an XML file and it contains bullet points 1 to 20. Here is example code:

foreach($xml->branches->branch->properties->property as $property) {
	if($property->bullet1 != '') $bullets .= "{$property->bullet1}\n";
}

That would read the first bullet point, and then we have $property->bullet2 etc etc. Obviously I could list all 20 like that and just go with $p->myField = $bullets but how would I make this into less code. I was thinking of a foreach within the above foreach

foreach(range(1, 20) as $bullet) {
	$li = '$property->bullet' . $bullet;
	$bullets .= $li;
}

And lots of variations of but thats drawing a blank.

Cheers as always people.

Link to comment
Share on other sites

14 minutes ago, Sergio said:

$i=1;
foreach($xml->branches->branch->properties->property as $property) {
	if($property->bullet1 != '' && $i <= 20) $bullets .= "{$property->bullet.$i}\n";
	$i++;
}

Like this or I am missing something?

Here is some example XML

<?xml version="1.0" encoding="utf-8"?>
  <branches>
    <branch>
      <properties>
        <property>
          <bullet1></bullet1>
          <bullet2></bullet2>
          <bullet3></bullet3>
          <bullet4></bullet4>
          <bullet5></bullet5>
          <bullet6></bullet6>
          <bullet7></bullet7>
          <bullet8></bullet8>
          <bullet9></bullet9>
          <bullet10></bullet10>
          <bullet11></bullet11>
          <bullet12></bullet12>
          <bullet13></bullet13>
          <bullet14></bullet14>
          <bullet15></bullet15>
          <bullet16></bullet16>
          <bullet17></bullet17>
          <bullet18></bullet18>
          <bullet19></bullet19>
          <bullet20></bullet20>
        </property>
      </properties>
    </branch>
  </branches>

Without testing your code would just increment the counter in every property, and not every bullet. I am doing each one per line for now, but still wondering how to get this down to a few lines.

Link to comment
Share on other sites

My bad. here's another approach:

Spoiler

<!DOCTYPE html>
<html>
<body>

<?php
$xmlString =
"<?xml version='1.0' encoding='UTF-8'?>
<branches>
<branch>
<properties>
<property>
<bullet1>text</bullet1>
<bullet2>text</bullet2>
<bullet3>text</bullet3>
<bullet4>text</bullet4>
<bullet5>text</bullet5>
<bullet6>text</bullet6>
<bullet7>text</bullet7>
<bullet8>text</bullet8>
<bullet9>text</bullet9>
<bullet10>text</bullet10>
<bullet11>text</bullet11>
<bullet12>text</bullet12>
<bullet13>text</bullet13>
<bullet14>text</bullet14>
<bullet15>text</bullet15>
<bullet16>text</bullet16>
<bullet17>text</bullet17>
<bullet18>text</bullet18>
<bullet19>text</bullet19>
<bullet20>text</bullet20>
</property>
</properties>
</branch>
</branches>";

$xml = simplexml_load_string($xmlString);
// print_r($xml);
$i=1;
$bullets = 'bullets are:\n\n';
foreach($xml->branch->properties->property->children() as $property){
	$bullets .= $property."\n";
    $i++;
}
echo $bullets;
?>


</body>
</html>

 

Link to comment
Share on other sites

1 hour ago, cb2004 said:

My bad this time, <property> contains more data so I wouldn't be able to just use children()

Spoiler

<!DOCTYPE html>
<html>
<body>

<?php
$xmlString =
"<?xml version='1.0' encoding='UTF-8'?>
<branches>
<branch>
<properties>
<property>
<title>title</title>
<id>999</id>
<bullet1>text</bullet1>
<bullet2>text</bullet2>
<bullet3>text</bullet3>
<bullet4>text</bullet4>
<bullet5>text</bullet5>
<bullet6>text</bullet6>
<bullet7>text</bullet7>
<bullet8>text</bullet8>
<bullet9>text</bullet9>
<bullet10>text</bullet10>
<bullet11>text</bullet11>
<bullet12>text</bullet12>
<bullet13>text</bullet13>
<bullet14>text</bullet14>
<bullet15>text</bullet15>
<bullet16>text</bullet16>
<bullet17>text</bullet17>
<bullet18>text</bullet18>
<bullet19>text</bullet19>
<bullet20>text</bullet20>
</property>
</properties>
</branch>
</branches>";

$xml = simplexml_load_string($xmlString);
$bullets = '';

function startsWith ($string, $startString) 
{ 
    $len = strlen($startString); 
    return (substr($string, 0, $len) === $startString); 
} 

foreach($xml->branch->properties->property->children() as $key => $value)
{
    if( startsWith( $key, 'bullet' ) ) {
        $bullets .= $value."\n";
    }
}

echo $bullets;
?>


</body>
</html>

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...