使用带有getElementsByTagName的php问题更新XML,并标识正确的子节点 [英] update XML using php issues with getElementsByTagName and identifying the correct childnode

查看:80
本文介绍了使用带有getElementsByTagName的php问题更新XML,并标识正确的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据用户提交的表单中的$ _POST变量来识别正确的XML节点。下面是我当前的XML,上面有一个注释:是否要放置新的XML数据,以及要获取表单数据并准备将其插入XML文档的PHP。

how do I identify the correct XML node based off a $_POST variable from a user submitted form. Below is my current XML with a note on were I want the new XML data to be placed and the PHP that takes the form data and prepares it to be inserted into the XML document.

XML:

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


<!-- The new article node will be placed inside of one of the content_sets child nodes. Either doc_types, video_types, image_types. -->
    <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>



    <image_types>
        <article>
            <doc_name>Test Image</doc_name>
            <file_name>logo.png</file_name>
            <doc_description>Logo transparent background. Please remove when live.</doc_description>
            <doc_tags>png,logo,logo png,no background,graphic,hi res</doc_tags>
            <last_update>01/26/2013 23:07</last_update>
        </article>
    </image_types>


</content_sets>

提交时的PHP:

$file_type = $_POST['file_type'];
//This is where the node name comes from

$doc = new DOMDocument();
$doc->load( 'rfp_files.xml' );

$doc->formatOutput = true;

$r = $doc->getElementsByTagName("content_sets")->getElementsByTagName($file_type);
*****//The above code is where my issue is coming from. I am not identifying the child node of content_sets correctly. 
$b = $doc->createElement("article");

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


$r->appendChild( $b );

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

我没有显示表格或其他条的子节点。如有需要,我可以发布更多代码。

I did not show the form or the rest of article's child nodes. If needed I can post more of my code.

推荐答案

使用 getElementsByTagName()时,您需要使用 item()方法,以便可以在节点列表中检索特定的节点-即使其中仅包含一项节点列表,您仍然必须执行此操作。

When using getElementsByTagName(), you need to use the item() method so you can retrieve a specific node in the node list - even if there is only one item in the node list, you still have to do this.

getElementsByTagName()将始终返回DOM节点列表,因此您必须遍历该列表,或者您必须通过item()方法检索特定项目-这样做有意义吗?这里有一个示例: http://php.net/manual/en/domnodelist.item .php

getElementsByTagName() will always return a DOM Node List, so you either have to loop through the list, or you have to retrieve a specific item via the item() method - does that make sense? There is an example here: http://php.net/manual/en/domnodelist.item.php

这篇关于使用带有getElementsByTagName的php问题更新XML,并标识正确的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆