Jump to content

Open, Edit and Save XML files with PHP


unvoider

Recommended Posts

Hello PHP Freak world,

 

I am trying to do exactly this: http://www.w3schools.com/Xsl/xsl_editxml.asp which uses ASP but I want to use PHP.

 

So far I am able to load an XML file into HTML and make a form but I am not sure how to have it take all the fields from the form and make it an XML file again.

 

Here is a snippet of how I am able to load the XML file into an HTML form:

<?php
            // Load XML file
            $XML = new DOMDocument();
            $XML->loadXML( $theData );
            
            // Start xslt
            $xslt = new XSLTProcessor();
            $XSL = new DOMDocument();
            $XSL->load( $XSLpath, LIBXML_NOCDATA);
            $xslt->importStylesheet( $XSL );
            
            // Print 
            print $xslt->transformToXML( $XML ); 
            ?>

 

My XSL file looks like this:

<!-- Host 1 -->
<xsl:for-each select="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/NAME">
<tr><td>HOST_1</td>
<td>NAME</td>
<td class="col3"><input type="text" style="width:100%;" name="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/NAME" >
<xsl:attribute name="id">
  <xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="value">
  <xsl:value-of select="." />
</xsl:attribute>
</input> 
</td>
</tr>
</xsl:for-each>

<xsl:for-each select="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/IP_ADDRESS">
<tr><td></td>
<td>IP_ADDRESS</td>
<td class="col3"><input type="text" style="width:100%;" name="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/IP_ADDRESS" >
<xsl:attribute name="id">
  <xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="value">
  <xsl:value-of select="." />
</xsl:attribute>
</input> 
</td>
</tr>
</xsl:for-each>

<xsl:for-each select="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/OS">
<tr><td></td>
<td>OS</td>
<td class="col3"><input type="text" style="width:100%" name="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/OS" >
<xsl:attribute name="id">
  <xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="value">
  <xsl:value-of select="." />
</xsl:attribute>
</input> 
</td>
</tr>
</xsl:for-each>

<xsl:for-each select="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/IO/HOTADD_DIR">
<tr><td></td>
<td>HOTADD_DIR</td>
<td class="col3"><input type="text" style="width:100%;" name="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/IO/HOTADD_DIR" >
<xsl:attribute name="id">
  <xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="value">
  <xsl:value-of select="." />
</xsl:attribute>
</input> 
</td>
</tr>
</xsl:for-each>

<xsl:for-each select="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/IO/SMUTIL_DIR">
<tr><td></td>
<td>SMUTIL_DIR</td>
<td class="col3"><input type="text" style="width:100%;" name="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/IO/SMUTIL_DIR" >
<xsl:attribute name="id">
  <xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="value">
  <xsl:value-of select="." />
</xsl:attribute>
</input> 
</td>
</tr>
</xsl:for-each>

<xsl:for-each select="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/IO/SMASH_EXE">
<tr><td></td>
<td>SMASH_EXE</td>
<td class="col3"><input type="text" style="width:100%;" name="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/IO/SMASH_EXE" >
<xsl:attribute name="id">
  <xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="value">
  <xsl:value-of select="." />
</xsl:attribute>
</input> 
</td>
</tr>
</xsl:for-each>

<xsl:for-each select="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/HBA[@RefName='HBA_1']/HBA_PORT[@RefName='HBA_PORT_1']/NAME">
<tr ID="HBAid0" span="3" style="display:none;"><td></td>
<td>HBA_1 NAME</td>
<td ID="td1" class="col3">
<input type="text" style="width:100%;" name="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/HBA[@RefName='HBA_1']/HBA_PORT[@RefName='HBA_PORT_1']/NAME" >
<xsl:attribute name="id">
  <xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="value">
  <xsl:value-of select="." />
</xsl:attribute>
</input> 
</td>
</tr>
</xsl:for-each>

<xsl:for-each select="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/HBA[@RefName='HBA_1']/HBA_PORT[@RefName='HBA_PORT_1']/WWN">
<tr ID="HBAid1" span="3" style="display:none;"><td></td>
<td>HBA_1 WWN</td>
<td class="col3"><input type="text" style="width:100%;" name="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/HBA[@RefName='HBA_1']/HBA_PORT[@RefName='HBA_PORT_1']/WWN" >
<xsl:attribute name="id">
  <xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="value">
  <xsl:value-of select="." />
</xsl:attribute>
</input> 
</td>
</tr>
</xsl:for-each>

<xsl:for-each select="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/HBA[@RefName='HBA_2']/HBA_PORT[@RefName='HBA_PORT_1']/NAME">
<tr ID="HBAid2" span="3" style="display:none;"><td></td>
<td>HBA_2 NAME</td>
<td class="col3"><input type="text" style="width:100%;" name="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/HBA[@RefName='HBA_2']/HBA_PORT[@RefName='HBA_PORT_1']/NAME" >
<xsl:attribute name="id">
  <xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="value">
  <xsl:value-of select="." />
</xsl:attribute>
</input> 
</td>
</tr>
</xsl:for-each>

<xsl:for-each select="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/HBA[@RefName='HBA_2']/HBA_PORT[@RefName='HBA_PORT_1']/WWN">
<tr ID="HBAid3" span="3" style="display:none;"><td></td>
<td>HBA_2 WWN</td>
<td class="col3"><input type="text" style="width:100%;" name="TEST_CONFIGURATION/HOST[@RefName='HOST_1']/HBA[@RefName='HBA_2']/HBA_PORT[@RefName='HBA_PORT_1']/WWN" >
<xsl:attribute name="id">
  <xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="value">
  <xsl:value-of select="." />
</xsl:attribute>
</input> 
</td>
</tr>
</xsl:for-each>

 

Any help, ideas, input or anything comments about doing this would be greatly appreciated.

 

thank you for your help,

Unvoider

Link to comment
Share on other sites

I'm still working on this. Any ideas? pointers? or someone that has done this before perhaps know of a way?

 

I am trying something like this:

//Loop through the form collection
foreach($_POST as $key => $value){
if ($value == 'Array') { 
	for ($i=0; $i<count($value); $i++){
		echo extract($value[$i]);
		echo "<br>";
	}
}
echo $key.": ".$value;
echo "<br>";
}
//
$extracted = extract($_POST);

 

But I am having problems with sub-tree nodes because they are put into an array which doesn't seem to get passed from one page to another.

Link to comment
Share on other sites

Here is a XML write, I wrote for a ticker feed. It displayed scores and team names, along with the date and type of sport. Maybe you can edit it to suit your needs. Also remember to give it 0777 permission

 

<?php
//Re-Written to show only rows with ticker set = 1 or whatever is in manual_ticker
$db_host = '?'; # The host of your server, normally localhost
$db_user = '?'; # The user name you use to connect to the server
$db_pass = '?'; # The password you use to connect
$db_name = '?'; # The name of a database
$fileName = "ticker/data.xml"; # The name of the XML file for the test2.swf
$dbh = mysql_connect ($db_host, $db_user, $db_pass) or die (mysql_error());
mysql_select_db ($db_name);	
function findTeam($id){
	$q = mysql_query("SELECT name FROM schools WHERE id = '$id'");
	if(mysql_num_rows($q) > 0){
		$f = mysql_fetch_array($q);
		return checkName($f['name']);
	}else{
		return checkName("Unknown Name");
	}
}
function checkName($name){
	if(strlen($name) > 12){
		return substr($name, 0, 12);
	}else{
		return $name;
	}
}
function noGameInfo(){
	$Info = "<item>\n";
	$Info .= "<team1>None</team1>\n";
	$Info .= "<team2>None</team2>\n";
	$Info .= "<score1>NA</score1>\n";
	$Info .= "<score2>NA</score2>\n";
	$Info .= "<date>NA</date>\n";
	$Info .= "<sport>No Games to Display.</sport>\n";
	$Info .= "</item>\n";
	return $Info;
}
function checkItem($item){
	if(empty($item) || !isset($item) || $item == null || $item == NULL && $item != 0){
		return "NA";
	}else{
		return $item;
	}
}
$fileInfo = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" charset=\"us-ascii\" ?>";
$fileInfo = "<images>\n";
$q = mysql_query("SELECT * FROM games WHERE ticker = '1'");
$n = mysql_num_rows($q);
$q2 = mysql_query("SELECT * FROM manual_ticker");
$n2 = mysql_num_rows($q2);
if($n > 0 || $n2 > 0){
	if($n > 0){
		while($f = mysql_fetch_array($q)){
			$fileInfo .= "<item>\n";
			$fileInfo .= "<team1>".findTeam($f['hsid'])."</team1>\n";
			$fileInfo .= "<team2>".findTeam($f['vsid'])."</team2>\n";
			$fileInfo .= "<score1>".checkItem($f['homescore'])."</score1>\n";
			$fileInfo .= "<score2>".checkItem($f['visitorscore'])."</score2>\n";
			$fileInfo .= "<date>".checkItem($f['date'])."</date>\n";
			$fileInfo .= "<sport>".checkItem($f['sport'])."</sport>\n";
			$fileInfo .= "</item>\n";
		}
	}
	if($n2 > 0){
		while($f = mysql_fetch_array($q2)){
			$fileInfo .= "<item>\n";
			$fileInfo .= "<team1>".findTeam($f['team1'])."</team1>\n";
			$fileInfo .= "<team2>".findTeam($f['team2'])."</team2>\n";
			$fileInfo .= "<score1>".checkItem($f['score1'])."</score1>\n";
			$fileInfo .= "<score2>".checkItem($f['score2'])."</score2>\n";
			$fileInfo .= "<date>".checkItem($f['date'])."</date>\n";
			$fileInfo .= "<sport>".checkItem($f['sport'])."</sport>\n";
			$fileInfo .= "</item>\n";
		}
	}
}else{
	$fileInfo .= noGameInfo();
}
$fileInfo .= '</images>';
$fileHandle = fopen($fileName, 'w') or die("Error Reading File: ".$fileName);
fwrite($fileHandle, $fileInfo);
fclose($fileHandle);
?>

Link to comment
Share on other sites

  • 2 weeks later...
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.