使用simpleXML生成xml

时间:2013-09-24 07:19:02

标签: php xml simplexml

我目前正在使用从数据库中获取的记录来创建动态XML生成器。我将对象从数据库中的查询返回到$ res_detail

Array 
( 
[0] => stdClass Object 
( 
    [MWSCID] => 2 
    [MWSCDID] => 1 
    [MWSCDName] => Root 
    [MWSCDValue] => message 
    [MWSCDRoot] => 1 
    [MWSCDisParent] => 1 
    [MWSCDTier] => 
    [MWSCDParent] => 
    [MWSCDNode] => 
    [MWSCDAttribute] => 
    [MWSCDDefaultValue] => 
    [MWSCDStatus] => 1 
) 
[1] => stdClass Object 
( 
    [MWSCID] => 2 
    [MWSCDID] => 2 
    [MWSCDName] => User ID 
    [MWSCDValue] => UserID 
    [MWSCDRoot] => 
    [MWSCDisParent] => 
    [MWSCDTier] => 
    [MWSCDParent] => 
    [MWSCDNode] => 1 
            [MWSCDDefinedValue] => 5
    [MWSCDAttribute] => 1 
    [MWSCDDefaultValue] => 1 
    [MWSCDStatus] => 1 
) 
[2] => stdClass Object 
( 
    [MWSCID] => 2 
    [MWSCDID] => 3 
    [MWSCDName] => Password 
    [MWSCDValue] => Password 
    [MWSCDRoot] => 
    [MWSCDisParent] => 
    [MWSCDTier] => 
    [MWSCDParent] => 
    [MWSCDNode] => 1 
            [MWSCDDefinedValue] => 6
    [MWSCDAttribute] => 1 
    [MWSCDDefaultValue] => 2 
    [MWSCDStatus] => 1 
) 
[3] => stdClass Object 
( 
    [MWSCID] => 2 
    [MWSCDID] => 4 
    [MWSCDName] => Document 
    [MWSCDValue] => Document 
    [MWSCDRoot] => 
    [MWSCDisParent] => 2 
    [MWSCDTier] => 1 
    [MWSCDParent] => 1 
    [MWSCDNode] => 
    [MWSCDAttribute] => 
    [MWSCDDefaultValue] => 
    [MWSCDStatus] => 1 
) 
[4] => stdClass Object 
( 
    [MWSCID] => 2 
    [MWSCDID] => 5 
    [MWSCDName] => Merchant ID 
    [MWSCDValue] => MerchantID 
    [MWSCDRoot] => 
    [MWSCDisParent] => 
    [MWSCDTier] => 
    [MWSCDParent] => 
    [MWSCDNode] => 2 
    [MWSCDAttribute] => 2 
    [MWSCDDefaultValue] => XXXXXX-5348-XXXX-XXXX-4a553aa6fe94 
    [MWSCDStatus] => 1 
) 
[5] => stdClass Object
( 
    [MWSCID] => 2 
    [MWSCDID] => 6 
    [MWSCDName] => Document Documentation 
    [MWSCDValue] => DocDefinition 
    [MWSCDRoot] => 
    [MWSCDisParent] => 
    [MWSCDTier] => 
    [MWSCDParent] => 
    [MWSCDNode] => 2 
    [MWSCDAttribute] => 2 
    [MWSCDDefaultValue] => XXXXXXXX-XXXX-4463-9EB0-36B992664AE4 
    [MWSCDStatus] => 1 
) 
[6] => stdClass Object 
( 
    [MWSCID] => 2 
    [MWSCDID] => 7 
    [MWSCDName] => Detail 
    [MWSCDValue] => Dtl 
    [MWSCDRoot] => 
    [MWSCDisParent] => 3 
    [MWSCDTier] => 
    [MWSCDParent] => 1 
    [MWSCDNode] => 
    [MWSCDAttribute] => 
    [MWSCDDefaultValue] => 
    [MWSCDStatus] => 1 
) 
[7] => stdClass Object 
( 
    [MWSCID] => 2 
    [MWSCDID] => 8 
    [MWSCDName] => Customer ID 
    [MWSCDValue] => CustID 
    [MWSCDRoot] => 
    [MWSCDisParent] => 
    [MWSCDTier] => 
    [MWSCDParent] => 
    [MWSCDNode] => 3 
    [MWSCDAttribute] => 3 
    [MWSCDDefaultValue] => XXXX-PP 
    [MWSCDStatus] => 1 
) 
)

*注意:MWSCDParent列:告知该节点属于哪个父

MWSCDisParent列:判断此节点是否为父节点

MWSCDValue列:将是节点名称

MWSCDAttribute列将指示记录是MWSCDNode的属性

我正在尝试使用此输出生成和XML:

<Message UserID="TestUser" Password="TestPassword">
  <Document MerchantID="XXXXXXXX-5348-XXXX-XXXX-4a553aa6fe94" DocDefinition="XXXXXXXX-XXXX-4463-9EB0-36B992664AE4">
    <Dtl CustID="XXXX-PP" RefID="></Dtl>
  </Document>
</Message>

我试过了:

$res_detail = $this->merchantwsconfigdetail_model->get_ws_detail(2);
$res_user = $this->merchantwslogin_model->user_list(' WHERE MWSCID='.$res_detail[0]->MWSCID,'','');        

$xmlDoc = new DOMDocument;

$xmlDoc->formatOutput = true;

for($a=0;$a<count($res_detail);$a++):
{
    if($res_detail[$a]->MWSCDRoot == "1"):
    {
        $root = $xmlDoc->appendChild($xmlDoc->createElement($res_detail[$a]->MWSCDValue));
        $out_xml = simplexml_import_dom($xmlDoc);

        for($b=0;$b<count($res_detail);$b++):
        {
            if(!empty($res_detail[$b]->MWSCDAttribute) && $res_detail[$b]->MWSCDNode == "1"):
            {
                $val = "";
                if(!empty($res_detail[$b]->MWSCDDefinedValue)):
                {
                    if($res_detail[$b]->MWSCDDefinedValue == "5"): //Username
                    {
                       $val = $res_user[0]->MWSLUsername;
                    }
                    elseif($res_detail[$b]->MWSCDDefinedValue == "6"):
                    {
                        $val = $res_user[0]->MWSLPassword;
                    }
                    endif;
                $attr[$b] = simplexml_import_dom($root);
                $attr[$b]->addAttribute($res_detail[$b]->MWSCDValue,$val);
                }
                endif;

            }
            endif;
        }
        endfor;
    }
    endif;
}
endfor;

        $ctr = 0;
        for($a=0;$a<count($res_detail);$a++):
        {
            if($res_detail[$a]->MWSCDRoot <> "1" && !empty($res_detail[$a]->MWSCDParent)):
            {
                $node[$ctr] = simplexml_import_dom($root);
                $node[$ctr]->addChild($res_detail[$a]->MWSCDValue)->addAttribute('test','tex');
                $ctr++;
            }
         }

我得到了:

<message password="Password" userid="TestUser">
    <document test="tex">
        <dtl test="tex"></dtl>
    </document>
</message>

这没关系,但问题出现的时候MWSCDParent中有多于1个看起来应该是这样的:

<a>
    <b attr="attrib_val1">
        <d attr="attrib_val1> *it goes deeper
            <e attr="attrib_val1>*here is the problem!? and the attribute is being inherited as well
            </e>
        </d>
    </b>
    <c> *problem is here. I cant pinpoint it to be a child as well
    </c>
</a>

我已将MWSCDParent设置为先前tocheck,如果它将更改对应于数据库中的值,但似乎

$node[$ctr] = simplexml_import_dom($root);
$node[$ctr]->addChild($res_detail[$a]->MWSCDValue)->addAttribute('test','tex');
$ctr++;

让孩子越走越深,我怎样才能保留父节点内的位置?或者如果我希望它在节点内更深入?

1 个答案:

答案 0 :(得分:0)

解决了这个问题,我注意到,解决了设置孩子的位置的问题,无论是在父母的内部还是另一个父母并且有孩子:

        $node[0] = $xmlDoc->createElement('message');
        $xmlDoc->appendChild($node[0]);

        $node[1] = $xmlDoc->createElement('document');
        $node[0]->appendChild($node[1]);

        $node[2] = $xmlDoc->createElement('dtl');
        $node[0]->appendChild($node[2]);

设置:

$node[X] = $xmlDoc->createElement('node_name'); //create the element
$node[Y]->appendchild($node[X]); // append or insert the $node[$X] to that node if using create element in node X it will produce a child, if createAttribute, it will add attributes into the node.

以及动态生成所需xml的代码:

        $xmlDoc = new DOMDocument('1.0');

        $xmlDoc->formatOutput = true;
        $root_name = "";
        $node = array();
        $node_ctr = "1";

        for($a=0;$a<count($res_detail);$a++):
        {
            /* GET ROOT NODE */
            if($res_detail[$a]->MWSCDRoot == "1"):
            {
                $node[0] = $xmlDoc->createElement($res_detail[$a]->MWSCDValue);
                $xmlDoc->appendChild($node[0]);
                for($b=0;$b<count($res_detail);$b++):
                {
                    /* GET ROOT ATTRIBUTE IF ANY */
                    if($res_detail[$b]->MWSCDAttribute == "1"):
                    {
                        $attr = $xmlDoc->createAttribute($res_detail[$b]->MWSCDValue);
                        $attr->value = $this->xml_functions->get_value($res_detail[$b]->MWSCDID,$res_detail,$res_user);
                        $node[0]->appendChild($attr);
                    }
                    endif;
                }
                endfor;
            }
            endif;

            if(!empty($node[0])):
            {
                if($res_detail[$a]->MWSCDParent == "1"):
                {
                    $node[$node_ctr] = $xmlDoc->createElement($res_detail[$a]->MWSCDValue);
                    $node[0]->appendChild($node[$node_ctr]);
                    $node_ctr++;
                    for($x=0;$x<count($res_detail);$x++):
                    {
                        if($res_detail[$x]->MWSCDAttribute == $node_ctr):
                        {
                            $attr = $xmlDoc->createAttribute($res_detail[$x]->MWSCDValue);
                            $attr->value = $this->xml_functions->get_value($res_detail[$x]->MWSCDID,$res_detail,$res_user);
                            $node[1]->appendChild($attr);
                        }
                        endif;
                    }
                    endfor;
                }
                elseif($res_detail[$a]->MWSCDParent <> "1" && !empty($res_detail[$a]->MWSCDParent)):
                {
                    $node[$node_ctr] = $xmlDoc->createElement($res_detail[$a]->MWSCDValue);
                    $node[$node_ctr-1]->appendChild($node[$node_ctr]);
                    $node_ctr++;
                    for($x=0;$x<count($res_detail);$x++):
                    {
                        if($res_detail[$x]->MWSCDAttribute == $node_ctr):
                        {
                            $attr = $xmlDoc->createAttribute($res_detail[$x]->MWSCDValue);
                            $attr->value = $this->xml_functions->get_value($res_detail[$x]->MWSCDID,$res_detail,$res_user);
                            $node[$node_ctr-1]->appendChild($attr);
                        }
                        endif;
                    }
                    endfor;

                }
                endif;
            }
            endif;
        }
        endfor;

输出:

<message password="Password" userid="TestUser">
    <document merchantid="" docdefinition="">
        <dtl ref5="" custid="" refid="" filldate="" ref1="" ref2="" ref3="" ref4="">    </dtl>
    </document>
</message>

*附加说明:我必须在查询中按列排序才能使其正常工作

    ORDER BY
`merchantwsconfigdetail`.`MWSCDRoot` DESC,
`merchantwsconfigdetail`.`MWSCDisParent` DESC,
`merchantwsconfigdetail`.`MWSCDParent` DESC,
`merchantwsconfigdetail`.`MWSCDNode` ASC,
`merchantwsconfigdetail`.`MWSCDAttribute` ASC