Jump to content

How can I get all files in a directory in to a array and then...


Z33M@N

Recommended Posts

How can I get all files in a directory in to a array and then use that array to set each file in to a domDocument load?

 

It will be used to output all the xml documents in a directory. Need direction please?

 

/**
*Get all XML files in the directory.
*/
$string="";
$fileCount=0;
$filePath=$PATH.'/xampp/htdocs/sae/xml/english/'; # Specify the path you want to look in. 
$dir = opendir($filePath); # Open the path
while ($file = readdir($dir)) { 
  if (eregi("\.xml",$file)) { # Look for files with a .xml extension
    $string .= "$file<br />";
$string[$fileCount];
    $fileCount++;

  }
}
//close the directory
closedir();

if ($fileCount > 0) {
  echo sprintf("<strong>List of Files in %s</strong><br />%s<strong>Total Files: %s</strong>",$filePath,$string,$fileCount);

$doc->load( 'xml/english/0-0-0-1001.xml' );

 

Thanks

Link to comment
Share on other sites

This should give you a good idea:

 

$dom = new DomDocument();
$root = $dom->createElement('files');

$directory = '';
foreach (glob("$directory/*.xml") as $file) {
   $element = $dom->createElement('file', $directory . DIRECTORY_SEPARATOR . $file);
   $element->setAttribute('dirname', $directory);
   $element->setAttribute('basename', $file);
   $element->setAttribute('extension', 'xml');
   $element->setAttribute('filename', pathinfo($file, PATHINFO_FILENAME));
   $root->appendChild($element);
}

$dom->appendChild($root);
print $dom->saveXml();

 

This extensive information (setAttribute()) is to use with XPath so that you can select all files with a certain extension for example later on with an XSLT-sheet:

 

//[@extension="xml"]

Link to comment
Share on other sites

Thanks

 

but I think I stated my question wrong!

 

I want to get all XML files from a directory, then I want all those XML files to be displayed with the dom. All files have the same structure.

 

:confused:

 

Care to share how these XML files then look like? And what the end-result should be?

Link to comment
Share on other sites

here is a sample of the xml

 

<RECORD id="0-0-0-11" number="11" version="94">
				<PARA style-name="pCenter-8" style-name-escaped="pCenter-8" style-id="0-0-0-53">
					<PICTURE id="0-0-0-25173-img" object-class="folio objects" name="SAE_Forward_09.jpg" href="#!-- #EXECUTIVE:HOME_PATH --#/img/SAE_Forward_09.jpg" width="6870" height="810" left-border-line-color="0" left-border-line-width="0" left-border-space="0" right-border-line-color="0" right-border-line-width="0" right-border-space="0" top-border-line-color="0" top-border-line-width="0" top-border-space="0" bottom-border-line-color="0" bottom-border-line-width="0" bottom-border-space="0"/>
				</PARA>
			</RECORD>
			<RECORD id="0-0-0-12" number="12" version="3">
				<PARA>
				</PARA>
			</RECORD>
			<RECORD id="0-0-0-16" number="13" version="84">
				<PARA>In 2009 het die wêreld weer stadig begin kop optel ná die globale resessie van die vorige jaar, maar kapitalisme se foutlyne is vir eens en altyd ontbloot, en dit was duidelik dat 'n volhoubare alternatief vir ongebreidelde verbruik gevind sou moet word. Die impak van die resessie - insluitende stygende werkloosheid, verergerende armoede en gekompromitteerde voedselsekuriteit - het 'n nuwe stel uitdagings vir beide ontwikkelende en ontwikkelde lande meegebring.</PARA>
			</RECORD>
			<RECORD id="0-0-0-250329" number="14" version="84">
				<PARA>Die Amerikaanse president, Barack Obama, wat in 2008 die Withuis binnegeseil het op 'n golf van optimisme, moes toesien hoedat sy steun wegsypel terwyl hy gesukkel het om sy verkiesingsbeloftes uit te voer te midde van die voortslepende resessie en die harde werklikheid van globale politiek. Verrassend genoeg is die Nobel-prys vir Vrede in 2009 aan Obama toegeken, maar in sy aanvaardingstoespraak moes hy toegee dat dit in sommige gevalle nodig was om oorlog te maak om vrede te behaal - klaarblyklik 'n verwysing na die plofbare situasie in die Midde-Ooste, veral in Afganistan, waar 'n nuwe inspuiting van Amerikaanse troepe die onttrekkingsplanne voorafgegaan het.</PARA>
			</RECORD>
			<RECORD id="0-0-0-250330" number="15" version="84">
				<PARA>Afrika het 'n kommerwekkende reeks staatsgrepe en voortslepende burgeroorloë beleef, maar aan die positiewe kant is die Libiese leier, Moeammar Ghaddafi, weerhou van 'n tweede termyn as president van die Afrika-Unie om sy planne vir 'n "Verenigde State van Afrika" onder sy leiding verder te voer. Vooruitgang is ook gemaak om Afrika-leiers wat misdade teen die mensdom gepleeg het, in plaaslike of internasionale howe aan die man te bring. In Zimbabwe het die Mugabe-Tsvangirai-koalisieregering egter steeds gesukkel om verligting vir sy mense te bring terwyl interne tweedrag voortgeduur het en internasionale sanksies in plek gebly het.</PARA>
			</RECORD>
			<RECORD id="0-0-0-250331" number="16" version="84">
				<PARA>Aan die tuisfront was 2009 die aanbreek van 'n nuwe politieke era toe Jacob Zuma vir Kgalema Motlanthe opgevolg het as Suid-Afrika se nuwe president nadat die NVA klagtes van korrupsie teen hom laat vaar het. Zuma het aan die werk gespring deur die Kabinet te herskommel om opvoeding, gesondheid, werkloosheid, grondhervorming en plattelandse ontwikkeling te takel, maar teen die einde van die jaar het impak van die resessie saamgespan met swak dienslewering en korrupsie om stakings en insidente van gewelddadige protes deur misnoegde burgers te laat plaasvind.</PARA>
			</RECORD>
			<RECORD id="0-0-0-250332" number="17" version="84">
				<PARA>Suid-Afrika betree 2010 met vuvuzelas gereed namate die 100-dae-aftel na die Fifa-sokkerwêreldbeker aanbreek. Die land - die eerste in Afrika om 'n Wêreldbeker aan te bied - was op skedule om 'n geraamde 500 000 besoekers te verwelkom by wat sonder twyfel een van die voorste sportgebeurtenisse op die planeet is.</PARA>
			</RECORD>

Link to comment
Share on other sites

And what is the desired end-result? That all files are one?

 

Yes, all the xml files data will then be stored in variables, example: $title $description, $pictureurl.

 

From there I need to store it in to a database table.

 

 

Link to comment
Share on other sites

And what make up the values of $title and $description?

 

$dom = new DomDocument();
$directory = '';
foreach (glob("$directory/*.xml") as $file) {
   $filepath = realpath("$directory/$file");
   if ($dom->load($filepath)) {
      $xpath = new DomXPath($dom);
      foreach ($xpath->query('//PARA') as $node) {
         //$title??
         $description = $node->nodeValue;
         if ($node->hasChildren()) {
            $imageUrl = $node->childNodes[0]->getAttribute('href');
         }
      }
   }
}

Link to comment
Share on other sites

And what make up the values of $title and $description?

 

$dom = new DomDocument();
$directory = '';
foreach (glob("$directory/*.xml") as $file) {
   $filepath = realpath("$directory/$file");
   if ($dom->load($filepath)) {
      $xpath = new DomXPath($dom);
      foreach ($xpath->query('//PARA') as $node) {
         //$title??
         $description = $node->nodeValue;
         if ($node->hasChildren()) {
            $imageUrl = $node->childNodes[0]->getAttribute('href');
         }
      }
   }
}

 

There is one more xml tag to that document and that is <HEADING>

 

<LEVEL style-name="7lv" style-name-escaped="lv" style-id="0-0-0-10" level-depth="2" toc-section="false">
	<RECORD id="0-0-0-9" number="9" version="3">
		<HEADING>Voorwoord</HEADING>
		<PARA style-name="pBold-Purple-11" style-name-escaped="pBold-Purple-11" style-id="0-0-0-44">
			<DESTINATION id="0-0-0-127" name="A_Sae_Voorwoord"/>Voorwoord</PARA>
	</RECORD>
	<LEVEL style-name="8lv" style-name-escaped="lv" style-id="0-0-0-13" level-depth="3" toc-section="false">
		<RECORD id="0-0-0-10" number="10" version="3">
			<HEADING>Die wêreld vandag</HEADING>
			<PARA style-name="pBold-Blue-11" style-name-escaped="pBold-Blue-11" style-id="0-0-0-37">Die wêreld vandag</PARA>
		</RECORD>

 

Think of the XML as an article. The article has a heading / title, Description / body, pictures.

 

so the title will be the <HEADING> tag, the body the <PARA> tag and the picture <PICTURE>.

 

thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.