使用PHP表单中的数据更新xml名称空间

qhhrdooz  于 2023-04-28  发布在  PHP
关注(0)|答案(2)|浏览(109)

在下面的示例中,PHP表单使用在字段中输入的文本更新XML。
XML文件标签。联系我们

<?xml version="1.0" encoding="UTF-8"?>
<inventors>
   <person>
      <name>change1</name>
      <comment>change2</comment>
   </person>
</inventors>

用于更改XML文件中的“change1”和“change2”的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");

 //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>

如何使用PHP从下面的XML结构中提取和更新字符串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>

在此提前感谢每一位愿意花时间帮助我的人

qyswt5oh

qyswt5oh1#

理想的做法是使用XPath从XML中提取数据。你将得到你想要的,通过以下步骤:
load.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');

form.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>

如果你还是不能,告诉我你哪部分有问题。我祝愿你们的项目取得成功。

n3h0vuf2

n3h0vuf22#

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

$xmlObject = new SimpleXMLElement($xmlString);

然后你就可以进入房产了。

相关问题