我目前正在使用从数据库中获取的记录来制作动态 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++;
让孩子越走越深,如何才能保留在父节点内的位置?或者如果我希望它在节点内更深入?