0

我目前正在使用从数据库中获取的记录来制作动态 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 Column:判断此节点是否为父节点

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 来检查它是否会根据数据库中的值而改变,但似乎

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

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

4

1 回答 1

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
于 2013-09-24T13:26:25.903 回答