如何用PHP提取并更新带命名空间XML中的指定字符串?
PHP表单更新无命名空间XML示例
以下示例展示PHP表单如何用输入框中的文本更新XML内容。
XML文件 labela.xml
<?xml version="1.0" encoding="UTF-8"?> <inventors> <person> <name>change1</name> <comment>change2</comment> </person> </inventors>
用于修改XML的PHP表单
<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"); // 获取person元素 $element = $xml->getElementsByTagName("person")->item(0); // 加载子元素 $name = $element->getElementsByTagName("name")->item(0); $comment = $element->getElementsByTagName("comment")->item(0); // 原代码中replaceChild属于冗余操作,可省略 ?> <?php if (isset($_POST["submit"])) { $name->nodeValue = $_POST["namanya"]; $comment->nodeValue = $_POST["commentnya"]; $xml->save("labela.xml"); } ?> <form method="POST" action=''> name <input type="text" value="<?php echo $name->nodeValue; ?>" name="namanya" /> comment <input type="text" value="<?php echo $comment->nodeValue; ?>" name="commentnya"/> <input name="submit" type="submit" /> </form>
(注:原示例中type="text-name"和type="text-comment"为无效输入类型,已修正为标准type="text";htmlentities($xml->save(...))无实际作用,已移除)
技术问询
现有如下带命名空间的XML结构,如何使用PHP提取并更新其中的change1、change2字符串?
<?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>
解决方案
带命名空间的XML无法用getElementsByTagName直接定位元素(该方法会忽略命名空间),必须使用DOMXPath配合命名空间映射实现精准定位。以下是完整的PHP表单实现:
<?php // 定义XML中用到的命名空间前缀与对应URI $namespaces = [ 'pt' => 'http://schemas.brother.info/ptouch/2007/lbx/main', 'barcode' => 'http://schemas.brother.info/ptouch/2007/lbx/barcode', 'text' => 'http://schemas.brother.info/ptouch/2007/lbx/text' ]; // 加载XML文档 $xml = new DOMDocument("1.0", "utf-8"); $xml->formatOutput = true; $xml->preserveWhiteSpace = false; $xml->load("label_with_namespace.xml"); // 替换为你的XML文件名 // 初始化XPath并注册命名空间 $xpath = new DOMXPath($xml); foreach ($namespaces as $prefix => $uri) { $xpath->registerNamespace($prefix, $uri); } // 定位目标pt:data元素 $dataChange1 = $xpath->query('//barcode:barcode/pt:data')->item(0); // 对应change1 $dataChange2 = $xpath->query('//text:text[1]/pt:data')->item(0); // 对应第一个text:text下的change2 // 处理表单提交 if (isset($_POST["submit"])) { if (!empty($_POST["change1"])) $dataChange1->nodeValue = $_POST["change1"]; if (!empty($_POST["change2"])) $dataChange2->nodeValue = $_POST["change2"]; $xml->save("label_with_namespace.xml"); // 保存修改后的XML } ?> <form method="POST" action=''> change1 <input type="text" value="<?php echo $dataChange1->nodeValue; ?>" name="change1" /> change2 <input type="text" value="<?php echo $dataChange2->nodeValue; ?>" name="change2"/> <input name="submit" type="submit" value="更新XML" /> </form>
关键说明:
- 命名空间注册:必须将XML中的前缀与对应URI注册到XPath,否则无法识别带前缀的元素
- XPath表达式:
//barcode:barcode/pt:data:全局匹配所有<barcode:barcode>下的<pt:data>(此处仅一个,对应change1)//text:text[1]/pt:data:全局匹配第一个<text:text>下的<pt:data>(对应change2),若要定位第三个<text:text>的change3,可改为//text:text[3]/pt:data
- 元素更新:直接修改元素的
nodeValue即可覆盖原有文本,无需额外替换操作
内容的提问来源于stack exchange,提问作者Danilo Fagundes
相关产品推荐
相关产品推荐

