php DomDocument save() 到 xml 的问题
trouble with php DomDocument save() to xml
我在使用 php domDocument 时遇到输出文件问题,输出是:
<node type="allow" tesst="text"/><node type="allow" text="test2"/>
我在意料之中
<node type="allow" tesst="text"/>
**<node type="allow" text="test2"/>
谁能帮帮我?
$xml = new DOMDocument("1.0","UTF-8");
$xml->load("../export/acl.conf.xml");
$rootTag = $xml->getElementsByTagName("list")->item(1);
$dataTag = $xml->createElement("node");
$domAttr = $xml->createAttribute('type');
$domAttr->value = "{$acl_type}";
$domAt = $xml->createAttribute('cidr');
$domAt->value = "{$ipadd}/32";
//$domComent = $xml->createComment($_REQUEST['comment']);
$dataTag->appendChild($domAttr);
$dataTag->appendChild($domAt);
$rootTag->appendChild($dataTag);
$xml->save("../export/acl.conf.xml");
您编辑了您的答案,但没有提供您正在处理的 XML 的示例!
也就是说,尝试结合使用 DOMText:
<?php
$xml = new DOMDocument();
$node1 = $xml->createElement("node");
$node1->setAttribute('anything', 'whatever');
$text = new DOMText('**');
$node2 = $xml->createElement("node");
$node2->setAttribute('another', 'example');
$xml->appendChild($node1);
$xml->appendChild($text);
$xml->appendChild($node2);
echo $xml->saveXML();
这将输出:
<?xml version="1.0"?>
<node anything="whatever"/>**<node another="example"/>
玩转它!祝你好运! https://3v4l.org/1kMMD
我在使用 php domDocument 时遇到输出文件问题,输出是:
<node type="allow" tesst="text"/><node type="allow" text="test2"/>
我在意料之中
<node type="allow" tesst="text"/>
**<node type="allow" text="test2"/>
谁能帮帮我?
$xml = new DOMDocument("1.0","UTF-8");
$xml->load("../export/acl.conf.xml");
$rootTag = $xml->getElementsByTagName("list")->item(1);
$dataTag = $xml->createElement("node");
$domAttr = $xml->createAttribute('type');
$domAttr->value = "{$acl_type}";
$domAt = $xml->createAttribute('cidr');
$domAt->value = "{$ipadd}/32";
//$domComent = $xml->createComment($_REQUEST['comment']);
$dataTag->appendChild($domAttr);
$dataTag->appendChild($domAt);
$rootTag->appendChild($dataTag);
$xml->save("../export/acl.conf.xml");
您编辑了您的答案,但没有提供您正在处理的 XML 的示例!
也就是说,尝试结合使用 DOMText:
<?php
$xml = new DOMDocument();
$node1 = $xml->createElement("node");
$node1->setAttribute('anything', 'whatever');
$text = new DOMText('**');
$node2 = $xml->createElement("node");
$node2->setAttribute('another', 'example');
$xml->appendChild($node1);
$xml->appendChild($text);
$xml->appendChild($node2);
echo $xml->saveXML();
这将输出:
<?xml version="1.0"?>
<node anything="whatever"/>**<node another="example"/>
玩转它!祝你好运! https://3v4l.org/1kMMD