PDA

View Full Version : deleting and orginizing xml data with php



tomthebomb231
11-06-2012, 07:33 PM
Alright so first off I need to warn you guys that I am a complete noob when it comes to coding. I've been learning how to do it from the internet for about a month now. I do not understand php very well, but I don't really want to, I just need to get something done.

Anyway right now I have a html page which displays 7 links. When you click on a link it will load an associated xml file in an iframe, which is styled by an xsl file, into a sortable table. I use a jquery plugin to sort the data. I also have a <div> below the table which contains a form which is used to update the original xml with new data using a php file (I am using uniServer to run the php files on my computer).

Since there are 7 links in the original html page, there are 7 different xml files, 7 different xsl files (each of these contain an html table and form), and 7 different php files.

Sorry if that was confusing.

So far everything works good, but there are 2 things I would like to do that I cannot seem to figure out myself or find any info (that I can understand) on the internet.

1. When I add data to an xml file using the form it comes out like


<backup><media_cartridge_label></media_cartridge_label><backup_tapeof></backup_tapeof><backup_date</backup_date><backup_type>d</backup_type><backup_description></backup_description></backup>

I want it to look like this


<backup>

<media_cartridge_label></media_cartridge_label>

<backup_tapeof></backup_tapeof>

<backup_date></backup_date>

<backup_type></backup_type>

<backup_description></backup_description>
</backup>

This way it would be much more easily readable and easier to edit in the future.

2. I would like to create a way to delete xml data (for example: something that would delete a certain <backup> tag and all the, tags and associated data, within that <backup> tag)

I'm am not sure of the best way to do this, that isn't overly complicated. I was thinking maybe I could just make another form that would have the user input a value for one of the tags that I would choose (not all the tags have unique values, but in all of the 7 links there is at least one tag that does. That tag would be the one I would require the user to enter). Then a php file would delete the tag which contained a matching value to what was entered in the form. all sibling tags with their associated values would be deleted as well as the parent tag (<backup> in this case).

This seems a little sloppy though and the user would have to enter the value exactly. So is there a better way to do this? And if there isn't, could someone help me with the php coding.
I really have no idea how php works. The only way I was able to make the xml file update was by searching the internet, copying what someone else had posted about doing the same thing, and then changing the tag names to be what I needed. Unfortunately I can't find anything on deleting data :(


thanks for any help in advance, and Sorry if this was confusing.

traq
11-06-2012, 08:49 PM
Unfortunately, there is not a copy-paste solution for this. PHP can exit XML, and if you want to try, I'd recommend looking at SimpleXML (http://php.net/simplexml) or DOMDocument (http://php.net/domdocument).

If you decide to give it a try, go ahead and post here and I'll be happy to help you figure out particular problems.

tomthebomb231
11-13-2012, 03:39 PM
Ok so I am thinking of a different way to do this. I am planning on including a check box on all the tables like this for example


<table border="1" id="myTable" class="tablesorter">
<thead>
<tr>
<th><b>Delete</b></th>
<th><b>Manufacturer</b></th>
<th><b>Model</b></th>
<th><b>Device ID</b></th>
<th><b>Function</b></th>
<th><b>Hostname</b></th>
<th><b>Admin IP</b></th>
<th><b>can-ssh</b></th>
<th><b>Location</b></th>
</tr>
</thead>
<tbody>
<xsl:for-each select="device">
<tr>
<td><form method="post" name="delete"><label for="delete"><input type="checkbox"/></label></form></td>
<td><b><xsl:value-of select="manufacturer"/></b></td>
<td><b><xsl:value-of select="model"/></b></td>
<td><b><xsl:value-of select="device_id"/></b></td>
<td><b><xsl:value-of select="function"/></b></td>
<td><b><xsl:value-of select="hostname"/></b></td>
<td><b><xsl:value-of select="admin_IP"/></b></td>
<td><b><xsl:value-of select="can-ssh"/></b></td>
<td><b><xsl:value-of select="location_street"/><xsl:text> </xsl:text><xsl:value-of select="location_city"/><xsl:text> </xsl:text><xsl:value-of select="location_state"/>, <xsl:value-of select="location_zip"/></b></td>
</tr>
</xsl:for-each>
</tbody>
</table>

Then if the user selected a checkbox, all the data in the corresponding horizontal row would be deleted. I am working with DOMDocument for the php code to do this since that's what I used earlier. I am just trying to think how I would get the form in the table to know what it was selecting, so I could send that selection off to the php file. Is that even possible?

Also does anyone know a solution to problem #1 from my first post?

thanks

tomthebomb231
11-13-2012, 04:34 PM
Here is the delete php code for one of the xmls


<?php

$doc = new DOMDocument;
$doc->load('network_inventory.xml');
$document->preserveWhiteSpace = false;



function deleteNode($device) {
deleteChildren($device);
$parent = $device->parentNode;
$oldnode = $parent->removeChild($device);
}
$doc->save("network_inventory.xml");
?>

and here is what the network_inventory.xml looks like


<network_inventory>
<device>
<manufacturer></manufacturer>
<model></model>
<device_id></device_id>
<function></function>
<hostname></hostname>
<admin_IP></admin_IP>
<can_ssh></can_ssh>
<location_street></location_street>
<location_city></location_city>
<location_state></location_state>
<location_zip></location_zip>
</device>
</network_inventory>


So I know that something is wrong with the php code. When I run it somehow changes the xml file because notepad++ tells me the data has been changed and needs to be reloaded, but nothing is different about the new xml.

I got this code form the comment section of http://www.php.net/manual/en/domnode.removechild.php
I tried every single one of the suggestions on that page but none of them worked?

Note: I am trying to delete one of the <device> nodes and all of it's child nodes. Which device node to delete is a different problem.

Thanks