0

Possible Duplicate:
How to modify xml file using PHP

I have an xml that looks like this:

<host host-name="www.1.com">
   <root-directory>c:/public_html</root-directory>
</host>
<host host-name="www.2.com">
   <root-directory>c:/public_html</root-directory>
</host>

I need do modify the xml using php like this

If the value of host-name = www.2.com

change the value of root-directory in c:/public_html/blahblah

So the final result would be:

<host host-name="www.1.com">
   <root-directory>c:/public_html</root-directory>
</host>
<host host-name="www.2.com">
   <root-directory>c:/public_html/blahblah</root-directory>
</host>

I really need some help. Thanks!

Update:

oh, i forgot to mention that i'm not a programmer, anyway here is what i've tried

$xmlDomain= "www.2.com";
$nomefile='myfile.xml';
$xmlDoc = new DomDocument();
$xmlDoc->load($nomefile); 
$Xroot = $xmlDoc->documentElement;
$products = $Xroot->getElementsByTagName("host");
$length = $products->length;
for ($i=$length-1;$i>=0;$i--)
{
$p = $products->item($i);    
$pid = $p->getAttribute("host-name");

if ($pid == $xmlDomain)
{  
$parent = $xmlDoc->getElementsByTagName("root-directory");     
$parent->nodeValue = 'c:/public_html/blahblah';             
}
}
$strxml = $xmlDoc->saveXML();
$handle = fopen($nomefile, "w");
fwrite($handle, $strxml);
fclose($handle);
Community
  • 1
  • 1
Adrian
  • 1
  • 2

1 Answers1

0

Okay, I needed to get my head away from writing documentation for a minute, so here it is:

function modifyRootDirectoryFormHosts(DOMDocument $doc, $hostNameToLookFor, $newRootDirectory)
{
    $xpath = new DOMXPath($doc);
    $items = $xpath->query(sprintf('//host[@host-name="%s"]', $hostNameToLookFor));

    if (count($items) > 0)
    {
        foreach($items as $item)
        {
            foreach($item->getElementsByTagName('root-directory') as $rootDirElement)
            {
                $rootDirElement->removeChild($rootDirElement->firstChild);
                $rootDirElement->appendChild($doc->createTextNode($newRootDirectory));
            }
        }
    }
}

$xmlSource = <<<XML
<root>
<host host-name="www.1.com">
   <root-directory>c:/public_html</root-directory>
</host>
<host host-name="www.2.com">
   <root-directory>c:/public_html</root-directory>
</host>
</root>
XML;


$doc = new DOMDocument();
$doc->loadXML($xmlSource);

modifyRootDirectoryFormHosts($doc, 'www.2.com', 'c:/public_html/blahblah');

echo $doc->saveXML();

output is:

<?xml version="1.0"?>
<root>
<host host-name="www.1.com">
   <root-directory>c:/public_html</root-directory>
</host>
<host host-name="www.2.com">
   <root-directory>c:/public_html/blahblah</root-directory>
</host>
</root>
Kris
  • 40,604
  • 9
  • 72
  • 101
  • hi Kris, here is the result :D any clues? – Adrian Sep 12 '12 at 14:44
  • That's the first line of an xml file. technically a processing instruction, telling xml parsers something about how to interpret the rest of the file. perfectly valid. – Kris Sep 12 '12 at 14:47
  • What I meant to say is thats the only line left after running the code, it deleted everything else. – Adrian Sep 12 '12 at 14:51
  • does your input file have a root tag? sounds like you didn't use the same code i did, or you're using invalid input xml. – Kris Sep 12 '12 at 15:02
  • sorry, i'm having some problems copying the code here. http://pastebin.com/nqZYKcRi – Adrian Sep 12 '12 at 15:07
  • and here your function adapted to my needs http://pastebin.com/vMhz3A4z – Adrian Sep 12 '12 at 15:14
  • you still haven't answered the root tag question, the xml in your original question was invalid so I had to add a root tag. does the file have one? is the file completely valid? – Kris Sep 13 '12 at 13:28
  • Kris i'm sorry but i'm having some problems copying the code here, here is the of the original xml http://pastebin.com/nqZYKcRi * this is the root tag – Adrian Sep 13 '12 at 14:46