Update xml namespace with data from PHP form
P粉445714413
2023-09-05 10:34:27
<p>In the example below, a PHP form updates the XML with the text entered in the field.</p>
<p>XML 文件 labela.xml:</p>
<pre class="brush:php;toolbar:false;"><?xml version="1.0" encoding="UTF-8"?>
<inventors>
<person>
<name>change1</name>
<comment>change2</comment>
</person>
</inventors></pre>
<p>用于更改 XML 文件中的“change1”和“change2”的 PHP 表单</p>
<pre class="brush:php;toolbar:false;"><script src="https://code.jquery.com/jquery-latest.min.js"></script>
<?php
$xml = new DOMDocument("1.0", "utf-8");
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load("labela.xml");
//Get item Element
$element = $xml->getElementsByTagName("person")->item(0);
//Load child elements
$name = $element->getElementsByTagName("name")->item(0);
$comment = $element->getElementsByTagName("comment")->item(0);
//Replace old elements with new
$element->replaceChild($name, $name);
$element->replaceChild($comment, $comment);
?>
<?php if (isset($_POST["submit"])) {
$name->nodeValue = $_POST["namanya"];
$comment->nodeValue = $_POST["commentnya"];
htmlentities($xml->save("labela.xml"));
} ?>
<form method="POST" action=''>
name <input type="text-name" value="<?php echo $name->nodeValue; ?>" name="namanya" />
comment <input type="text-comment" value="<?php echo $comment->nodeValue; ?>" name="commentnya"/>
<input name="submit" type="submit" />
</form></pre>
<p>如何使用 PHP 从下面的 XML 结构中提取并更新字符串change1 和change2?</p>
<pre class="brush:php;toolbar:false;"><?xml version="1.0" encoding="UTF-8"?>
<pt:document xmlns:pt="http://schemas.brother.info/ptouch/2007/lbx/main" xmlns:barcode="http://schemas.brother.info/ptouch/2007/lbx/barcode" xmlns:style="http://schemas.brother.info/ptouch/2007/lbx/style" xmlns:text="http://schemas.brother.info/ptouch/2007/lbx/text">
<pt:body currentSheet="Folha 1">
<style:sheet name="Folha 1">
<pt:objects>
<barcode:barcode>
<barcode:qrcodeStyle model="2" eccLevel="15%" />
<pt:data>change1</pt:data>
</barcode:barcode>
<text:text>
<text:textStyle vertical="false" />
<pt:data>change2</pt:data>
<text:stringItem charLen="7">
<text:ptFontInfo>
<text:logFont name="Arial" />
</text:ptFontInfo>
</text:stringItem>
</text:text>
<text:text>
<text:textStyle vertical="false" />
<pt:data>change3</pt:data>
</text:text>
</pt:objects>
</style:sheet>
</pt:body>
</pt:document></pre>
<p>提前感谢所有花时间提供帮助的人</p>
I think the XML structure needs to be converted into an object
You can then access the property.
Ideally use XPath to extract data from XML. By doing the following you will get the results you want:
Load.php
Form.php