【问题标题】:Editing an attribute in XML using PHP DOM使用 PHP DOM 编辑 XML 中的属性
【发布时间】:2021-11-22 15:56:45
【问题描述】:
<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 更新它。我想将&lt;mac address='02:00:00:bc:d6' /&gt; 这个属性值更新为其他值。请指导我该怎么做?在过去的几个小时里,我尝试了许多不同的代码。我正在运行下面提到的代码。我想使用 $mainmac 更新 XML 文件的 mac 地址值。请帮忙。

$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 xml-parsing dom-events


    【解决方案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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      相关资源
      最近更新 更多