0

I am trying to insert data into my XML document into a specific node of content_set I thought I had to use item() but every time I submit my form the data gets put in at the end of my document but before the closing content_sets

PHP:

//This is where I thought I would choose what node the data is put into based on the value of the select in my html, with 0 being the doc_types and 1 being video_types

$file_type = $_POST['file_type'];
$doc = new DOMDocument();
$doc->load( 'myfile_files.xml' );

$doc->formatOutput = true;
$r = $doc->getElementsByTagname('content_sets')->item($file_type);

$b = $doc->createElement("article");

$titleName = $doc->createElement("doc_name");
$titleName->appendChild(
$doc->createTextNode( $Document_Array["name"] )
);

$b->appendChild( $titleName );


$r->appendChild( $b );

$doc->save("myfile_files.xml");

XML:

<?xml version="1.0" encoding="UTF-8"?>
<content_sets>
    <doc_types>

        <article>
            <doc_name>Test Proposal</doc_name>
            <file_name>tes_prop.docx</file_name>
            <doc_description>Test Word document. Please remove when live.</doc_description>
            <doc_tags>word document,test,rfp template,template,rfp</doc_tags>
            <last_update>01/26/2013 23:07</last_update>
        </article>
    </doc_types>



    <video_types>
        <article>
            <doc_name>Test Video</doc_name>
            <file_name>test_video.avi</file_name>
            <doc_description>Test video. Please remove when live.</doc_description>
            <doc_tags>test video,video, avi,xvid,svid avi</doc_tags>
            <last_update>01/26/2013 23:07</last_update>
        </article>
    </video_types>
</content_sets>

HTML:

 <p>Content Type:<br/>
<select name="file_type">
    <option value="0">Document</option>
    <option value="1">Video</option>
    <option value="2">Image</option>
</select></p>

I thried to include the most imporant parts of the script but can post all of it if needed.

Thanks!

Denoteone
  • 4,043
  • 21
  • 96
  • 150
  • be more specific: (1) which `` is the one to be changed? (2) which element do you want to insert? – michi Apr 23 '13 at 10:50

1 Answers1

1

1) Use XPath to find the node you wish to change

2) Once you have the node, simply assign a new value to it.

3) Write the file when you're done

Check out these links for more details:

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks for the information. I made a edit to my php to show the rest of the xml update and save. I am just wondering why my original script didn't work if use getElementsByTagname to find all the nodes of content_sets and then find the first one using item(0) to insert? Thanks again! – Denoteone Apr 22 '13 at 05:44
  • Q: Why didn't getElementsByTagname() work? A: There are lots of possible reasons. If you want to debug, I'd break your complex expression down into subexpressions, then "printf" the value of each subexpression until you find the error. For example, `echo $file_type
    `, then `print_r ($doc->getElementsByTagname('content_sets'))
    `, etc. You might also consider getting an IDE like PHP for Eclipse, or trying [XDebug](http://www.xdebug.org/docs/all).
    – paulsm4 Apr 22 '13 at 06:39