[英]Generate xml using simpleXML
I am currently making a dynamic XML generator by using records I get from the database. 我目前正在使用从数据库中获取的记录来制作动态XML生成器。 I have object returned from a query in the database into $res_detail
我有从数据库中的查询返回的对象进入$ 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
)
)
*NOTE: MWSCDParent Column: Tells which parent this node belongs to *注意:MWSCDParent列:告诉此节点属于哪个父节点
MWSCDisParent Column: Tells if this node is a parent MWSCDisParent列:告诉此节点是否为父节点
MWSCDValue Column: Will be the Node name MWSCDValue列:将是节点名称
MWSCDAttribute column will indicate the record is an attribute for the MWSCDNode MWSCDAttribute列将指示记录是MWSCDNode的属性
I am trying to make it to produce and XML with this output: 我正在尝试使用以下输出来生成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>
I have tried: 我努力了:
$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++;
}
}
I am getting: 我正进入(状态:
<message password="Password" userid="TestUser">
<document test="tex">
<dtl test="tex"></dtl>
</document>
</message>
which is ok, but the problem arise is when the there is more than 1 in MWSCDParent which should look something like this: 没关系,但问题是当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>
I have set the MWSCDParent earlier tocheck if it will change corresponding to the value in the database, but it seems that 我之前已经设置了MWSCDParent来检查它是否会根据数据库中的值而改变,但是似乎
$node[$ctr] = simplexml_import_dom($root);
$node[$ctr]->addChild($res_detail[$a]->MWSCDValue)->addAttribute('test','tex');
$ctr++;
makes the child to go deeper and deeper, how can I retain the position within the parent's node? 使孩子越来越深入,如何在父级节点内保留位置? or if I want it to go deeper within the node?
还是如果我希望它深入节点内?
Solved the problem, I noticed, to solve the problem of setting the position of the child whether to be inside the parent or to be another parent and have child: 我注意到,解决了该问题,以解决将孩子的位置设置为在父母内部还是其他父母并生孩子的问题:
$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]);
set the: 设置:
$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.
and the code to make the required xml to be dynamically generated: 以及使所需的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;
OUTPUT: 输出:
<message password="Password" userid="TestUser">
<document merchantid="" docdefinition="">
<dtl ref5="" custid="" refid="" filldate="" ref1="" ref2="" ref3="" ref4=""> </dtl>
</document>
</message>
*ADDITIONAL NOTE: I had to ORDER BY the column in the query to make it work *附加说明:我必须对查询中的列进行排序才能使其正常工作
ORDER BY
`merchantwsconfigdetail`.`MWSCDRoot` DESC,
`merchantwsconfigdetail`.`MWSCDisParent` DESC,
`merchantwsconfigdetail`.`MWSCDParent` DESC,
`merchantwsconfigdetail`.`MWSCDNode` ASC,
`merchantwsconfigdetail`.`MWSCDAttribute` ASC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.