使用 PHP 表单中的数据更新 xml 命名空间
P粉445714413
P粉445714413 2023-09-05 10:34:27
[PHP讨论组]
<p>在下面的示例中,PHP 表单使用字段中输入的文本更新 XML。</p> <p>XML 文件 labela.xml:</p> <pre class="brush:php;toolbar:false;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;inventors&gt; &lt;person&gt; &lt;name&gt;change1&lt;/name&gt; &lt;comment&gt;change2&lt;/comment&gt; &lt;/person&gt; &lt;/inventors&gt;</pre> <p>用于更改 XML 文件中的“change1”和“change2”的 PHP 表单</p> <pre class="brush:php;toolbar:false;">&lt;script src=&quot;https://code.jquery.com/jquery-latest.min.js&quot;&gt;&lt;/script&gt; &lt;?php $xml = new DOMDocument(&quot;1.0&quot;, &quot;utf-8&quot;); $xml-&gt;formatOutput = true; $xml-&gt;preserveWhiteSpace = false; $xml-&gt;load(&quot;labela.xml&quot;); //Get item Element $element = $xml-&gt;getElementsByTagName(&quot;person&quot;)-&gt;item(0); //Load child elements $name = $element-&gt;getElementsByTagName(&quot;name&quot;)-&gt;item(0); $comment = $element-&gt;getElementsByTagName(&quot;comment&quot;)-&gt;item(0); //Replace old elements with new $element-&gt;replaceChild($name, $name); $element-&gt;replaceChild($comment, $comment); ?&gt; &lt;?php if (isset($_POST[&quot;submit&quot;])) { $name-&gt;nodeValue = $_POST[&quot;namanya&quot;]; $comment-&gt;nodeValue = $_POST[&quot;commentnya&quot;]; htmlentities($xml-&gt;save(&quot;labela.xml&quot;)); } ?&gt; &lt;form method=&quot;POST&quot; action=''&gt; name &lt;input type=&quot;text-name&quot; value=&quot;&lt;?php echo $name-&gt;nodeValue; ?&gt;&quot; name=&quot;namanya&quot; /&gt; comment &lt;input type=&quot;text-comment&quot; value=&quot;&lt;?php echo $comment-&gt;nodeValue; ?&gt;&quot; name=&quot;commentnya&quot;/&gt; &lt;input name=&quot;submit&quot; type=&quot;submit&quot; /&gt; &lt;/form&gt;</pre> <p>如何使用 PHP 从下面的 XML 结构中提取并更新字符串change1 和change2?</p> <pre class="brush:php;toolbar:false;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;pt:document xmlns:pt=&quot;http://schemas.brother.info/ptouch/2007/lbx/main&quot; xmlns:barcode=&quot;http://schemas.brother.info/ptouch/2007/lbx/barcode&quot; xmlns:style=&quot;http://schemas.brother.info/ptouch/2007/lbx/style&quot; xmlns:text=&quot;http://schemas.brother.info/ptouch/2007/lbx/text&quot;&gt; &lt;pt:body currentSheet=&quot;Folha 1&quot;&gt; &lt;style:sheet name=&quot;Folha 1&quot;&gt; &lt;pt:objects&gt; &lt;barcode:barcode&gt; &lt;barcode:qrcodeStyle model=&quot;2&quot; eccLevel=&quot;15%&quot; /&gt; &lt;pt:data&gt;change1&lt;/pt:data&gt; &lt;/barcode:barcode&gt; &lt;text:text&gt; &lt;text:textStyle vertical=&quot;false&quot; /&gt; &lt;pt:data&gt;change2&lt;/pt:data&gt; &lt;text:stringItem charLen=&quot;7&quot;&gt; &lt;text:ptFontInfo&gt; &lt;text:logFont name=&quot;Arial&quot; /&gt; &lt;/text:ptFontInfo&gt; &lt;/text:stringItem&gt; &lt;/text:text&gt; &lt;text:text&gt; &lt;text:textStyle vertical=&quot;false&quot; /&gt; &lt;pt:data&gt;change3&lt;/pt:data&gt; &lt;/text:text&gt; &lt;/pt:objects&gt; &lt;/style:sheet&gt; &lt;/pt:body&gt; &lt;/pt:document&gt;</pre> <p>提前感谢所有花时间提供帮助的人</p>
P粉445714413
P粉445714413

全部回复(2)
P粉938936304

我认为需要将XML结构转换为对象

$xmlObject = new SimpleXMLElement($xmlString);

然后您就可以访问该属性。

P粉564301782

理想的情况是使用 XPath 从 XML 中提取数据。 通过执行以下操作,您将获得您想要的结果:

加载.php

<?php
$change1 = $_POST['change1'];
$change2 = $_POST['change2'];
$change3 = $_POST['change3'];   
$xml = simplexml_load_file('your_complex_xml.xml');
$xml->xpath('//pt:data')[0][0] = $change1;
$xml->xpath('//pt:data')[1][0] = $change2;
$xml->xpath('//pt:data')[2][0] = $change3;
$xml->asXML('new_complex_xml.xml');

表单.php

    <form method="post" action="load.php">
    <label for="change1">change1:</label>
    <input type="text" name="change1" id="change1"><br>
    <label for="change2">change2:</label>
    <input type="text" name="change2" id="change2"><br>
    <label for="change3">change3:</label>
    <input type="text" name="change3" id="change3"><br>
    <input type="submit" value="Generate New XML">
    </form>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号