RockingPanda
RockingPanda

Reputation: 9

Editing an attribute in XML using PHP DOM

<domain type='kvm'>
  <name>v3748</name>
  <uuid>765c47b4-9279-4d75-9029-c0231839ed62</uuid>
  <memory>2097152</memory>
  <currentMemory>2097152</currentMemory>
  <vcpu >2</vcpu>
  <cpu mode='host-model'>

   </cpu>
  <cputune>
    <shares>1024</shares>
    <period>100000</period>
    <quota>70000</quota>
    <emulator_period>100000</emulator_period>
    <emulator_quota>70000</emulator_quota>
  </cputune>
  <os>
    <type arch='x86_64' machine='pc'>hvm</type>
    <boot dev='hd'/>
        <bootmenu enable='yes'/>
  </os>
  <features>
    <acpi/>
    <apic/>
 </features><clock offset='utc'>
        <timer name='pit' tickpolicy='delay'/>
        <timer name='rtc' tickpolicy='catchup'/>
        <timer name='hpet' present='no'/>
  </clock>

  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
<emulator>/usr/bin/kvm</emulator>

        <disk type='file' device='disk'>
        <driver name='qemu' type='raw' cache='none' />
        <source file='/kvm/v3748-d3osjjbeqbxctjcg-0meompahicty80ng.raw'/>
          <target dev='vda' bus='virtio' />

    </disk>

        <interface type='bridge'>
        <source bridge='viifbr0' />
        <target dev='viifv3748'/>
        <model type='virtio' />
        <mac address='02:00:00:bc:d6' />

        </interface>

    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target port='0'/>
    </console><sound model='ich6'>
        <codec type='micro'/>
    </sound>

    <input type='tablet' bus='usb'/>
    <input type='mouse' bus='ps2'/>    <graphics type='vnc' port='5974' autoport='no' listen='' passwd='1zcj7k6zrb' keymap='en-us'/>
    </devices>

I have an xml file and I want to update it using the PHP DOM. I want to update <mac address='02:00:00:bc:d6' /> this attribute value to something else. Please guide how can I do this? I have tried many different codes in the last few hours. I am running the code mentioned below. I want to update the XML file mac address value using $mainmac. Please help.

$doc = new DOMDocument;
$doc->load($path);

$root = $doc->documentElement;
$devices = $root->getElementsByTagName('devices')->item(0);

$mac = $doc->createElement('mac', $mainmac);
        $mac_attr = $doc->createAttribute('address');
        $mac_attr->value = 'MAC_ADDRESS';
        $mac->appendChild($mac_attr);
        $interface->appendChild($mac);

$devices->appendChild($interface);


$doc->save($path);

Upvotes: 0

Views: 151

Answers (1)

ThW
ThW

Reputation: 19512

You can use Xpath to fetch the attribute node and set its value:

$xml = <<<'XML'
<domain type='kvm'>
  <name>v3748</name>
  <devices>
    <interface type='bridge'>
      <model type='virtio' />
      <mac address='02:00:00:bc:d6' />
    </interface>
  </devices>
</domain>
XML;

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);

foreach ($xpath->evaluate('//mac/@address[. = "02:00:00:bc:d6"]') as $address) {
    $address->value = 'replacement';
}

echo $document->saveXML();

Output:

<?xml version="1.0"?>
<domain type="kvm">
  <name>v3748</name>
  <devices>
    <interface type="bridge">
      <model type="virtio"/>
      <mac address="replacement"/>
    </interface>
  </devices>
</domain>

Or you fetch the element node and call setAttribute().

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);

foreach ($xpath->evaluate('//mac[@address = "02:00:00:bc:d6"]') as $mac) {
    $mac->setAttribute('address', 'replacement');
}

echo $document->saveXML();

Upvotes: 1

Related Questions