Heim  >  Fragen und Antworten  >  Hauptteil

Aktualisieren Sie den XML-Namespace mit Daten aus dem PHP-Formular

<p>Im folgenden Beispiel aktualisiert ein PHP-Formular das XML mit dem in das Feld eingegebenen Text.</p> <p>XML-Datei labela.xml:</p> <pre class="brush:php;toolbar:false;"><?xml version="1.0" Encoding="UTF-8"?> <Erfinder> <Person> <name>change1</name> <comment>change2</comment> </person> </inventors></pre> <p>用于更改 XML-Format „change1“ und „change2“ und PHP-Format</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"); //Item-Element abrufen $element = $xml->getElementsByTagName("person")->item(0); //Untergeordnete Elemente laden $name = $element->getElementsByTagName("name")->item(0); $comment = $element->getElementsByTagName("comment")->item(0); //Alte Elemente durch neue ersetzen $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 <Eingabetyp="Textname" value="<?php echo $name->nodeValue; ?>" name="namanya" /> Kommentar <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>
P粉445714413P粉445714413383 Tage vor486

Antworte allen(2)Ich werde antworten

  • P粉938936304

    P粉9389363042023-09-06 09:29:53

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

    $xmlObject = new SimpleXMLElement($xmlString);

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

    Antwort
    0
  • P粉564301782

    P粉5643017822023-09-06 00:47:48

    理想的情况是使用 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>

    Antwort
    0
  • StornierenAntwort