使用 PHP DOM 在 XML 中编辑属性

Editing an attribute in XML using PHP DOM

提问人:RockingPanda 提问时间:10/1/2021 更新时间:10/1/2021 访问量:134

问:

<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>

我有一个 xml 文件,我想使用 PHP DOM 更新它。我想将此属性值更新为其他值。请指导我该怎么做?在过去的几个小时里,我尝试了许多不同的代码。我正在运行下面提到的代码。我想使用 $mainmac 更新 XML 文件 mac 地址值。请帮忙。<mac address='02:00:00:bc:d6' />

$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);
php xml 解析 dom-events

评论


答:

1赞 ThW 10/1/2021 #1

您可以使用 Xpath 获取属性节点并设置其值:

$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();

输出:

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

或者你获取元素节点并调用 .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();