I am currently making a dynamic XML generator by using records I get from the database. I have object returned from a query in the database into $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
MWSCDisParent Column: Tells if this node is a parent
MWSCDValue Column: Will be the Node name
MWSCDAttribute column will indicate the record is an attribute for the MWSCDNode
I am trying to make it to produce and XML with this output:
<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:
<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
$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:
$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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.