Inserting data in XML file with PHP DOM

后端 未结 6 1525
南旧
南旧 2020-12-18 06:03

I was trying to insert new data into an existing XML file, but it\'s not working. Here\'s my xml file:


    swimming

        
相关标签:
6条回答
  • 2020-12-18 06:37

    $newText = $xmldoc->createTextNode($newActv);

    Change this line to

    $newText = $xmldoc->createTextNode($newAct);

    0 讨论(0)
  • 2020-12-18 06:40

    I think I know what is the problem with your code. You should not write like that: <?xml-stylesheet type="text/xsl" href="sample.xsl" ?> The right code is:

    <?xml:stylesheet type="text/xsl" href="sample.xsl" ?>
    
    0 讨论(0)
  • 2020-12-18 06:45

    is your code block copy and pasted from your existing files? if so i see two potential issues:

    <form name='input' action'insert.php' method='post'> // should be:
    <form name="input" action="insert.php" method="post">
    

    note: you're missing action="insert.php", which would cause the form to just reload itself without submitting, which is the behaviour you describe.

    secondly, make sure you have write permission to "sample.xml". you can confirm if you're actually writing anything:

    print 'I wrote '.$xmldoc->save('sample.xml').' bytes of data';
    
    0 讨论(0)
  • 2020-12-18 06:45

    Final Solution

    sample.XML

    <list>
        <activity>swimming</activity>
        <activity>running</activity>
        <activity>Jogging</activity>
        <activity>Theatre</activity>
        <activity>Programming</activity>
    </list>
    

    index.php

    <html>
    <head><title>test</title></head>
    </head>
    
    <?php
        $xmldoc = new DOMDocument();
        $xmldoc->load("sample.xml", LIBXML_NOBLANKS);
    
        $activities = $xmldoc->firstChild->firstChild;
        if($activities!=null){
            while($activities!=null){
                echo $activities->textContent."<br/>";
                $activities = $activities->nextSibling;
            }
        }
    ?>
    
    <form name="input" action="insert.php" method="post">
        insert activity:
        <input type="text" name="activity"/>
        <input type="submit" value="send"/>
    </form>
    </body>
    </html>
    

    insert.php

    <?php
        header('Location:index.php');
        $xmldoc = new DOMDocument();
        $xmldoc->load('sample.xml');
    
        $newAct = $_POST['activity'];
    
        $root = $xmldoc->firstChild;
    
        $newElement = $xmldoc->createElement('activity');
        $root->appendChild($newElement);
        $newText = $xmldoc->createTextNode($newAct);
        $newElement->appendChild($newText);
    
        $xmldoc->save('sample.xml');
    ?>
    
    0 讨论(0)
  • 2020-12-18 06:49

    Actually you made mistakes in two places.

    This line should be I think because of the typo, you missed an equal sign. Also

    These lines should be

    Try now, it should work, Hop this would make some sense

    0 讨论(0)
  • 2020-12-18 06:50

    this is the code i work for me.

    index.php

    <html>
    <head><title>test</title></head>
    </head>
    
    <?php
        $xmldoc = new DOMDocument();
        $xmldoc->load('sample.xml', LIBXML_NOBLANKS);
    
        $activities = $xmldoc->firstChild->firstChild;
        if($activities!=null){
            while($activities!=null){
                echo $activities->textContent.'<br/>';
                $activities = $activities->nextSibling;
            }
        }
    ?>
    
    <form name='input' action='insert.php' method='post'>
        insert activity:
        <input type='text' name='activity'/>
        <input type='submit' value='send'/>
    </form>
    </body>
    </html>
    
    
    
    
    insert.php
    
    
    <?php
        header('Location:index.php');
        $xmldoc = new DOMDocument();
        $xmldoc->load('sample.xml');
    
        $newAct = $_POST['activity'];
    
        $root = $xmldoc->firstChild;
    
        $newElement = $xmldoc->createElement('activity');
        $root->appendChild($newElement);
        $newText = $xmldoc->createTextNode($newAct);
        $newElement->appendChild($newText);
    
        $xmldoc->save('sample.xml');
    ?>
    

    sample.xml

    <list>
      <activity>swimming</activity> 
      <activity>running</activity> 
    </list>
    
    0 讨论(0)
提交回复
热议问题