Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate xml using simpleXML
    primarykey
    data
    text
    <p>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</p> <pre><code>Array ( [0] =&gt; stdClass Object ( [MWSCID] =&gt; 2 [MWSCDID] =&gt; 1 [MWSCDName] =&gt; Root [MWSCDValue] =&gt; message [MWSCDRoot] =&gt; 1 [MWSCDisParent] =&gt; 1 [MWSCDTier] =&gt; [MWSCDParent] =&gt; [MWSCDNode] =&gt; [MWSCDAttribute] =&gt; [MWSCDDefaultValue] =&gt; [MWSCDStatus] =&gt; 1 ) [1] =&gt; stdClass Object ( [MWSCID] =&gt; 2 [MWSCDID] =&gt; 2 [MWSCDName] =&gt; User ID [MWSCDValue] =&gt; UserID [MWSCDRoot] =&gt; [MWSCDisParent] =&gt; [MWSCDTier] =&gt; [MWSCDParent] =&gt; [MWSCDNode] =&gt; 1 [MWSCDDefinedValue] =&gt; 5 [MWSCDAttribute] =&gt; 1 [MWSCDDefaultValue] =&gt; 1 [MWSCDStatus] =&gt; 1 ) [2] =&gt; stdClass Object ( [MWSCID] =&gt; 2 [MWSCDID] =&gt; 3 [MWSCDName] =&gt; Password [MWSCDValue] =&gt; Password [MWSCDRoot] =&gt; [MWSCDisParent] =&gt; [MWSCDTier] =&gt; [MWSCDParent] =&gt; [MWSCDNode] =&gt; 1 [MWSCDDefinedValue] =&gt; 6 [MWSCDAttribute] =&gt; 1 [MWSCDDefaultValue] =&gt; 2 [MWSCDStatus] =&gt; 1 ) [3] =&gt; stdClass Object ( [MWSCID] =&gt; 2 [MWSCDID] =&gt; 4 [MWSCDName] =&gt; Document [MWSCDValue] =&gt; Document [MWSCDRoot] =&gt; [MWSCDisParent] =&gt; 2 [MWSCDTier] =&gt; 1 [MWSCDParent] =&gt; 1 [MWSCDNode] =&gt; [MWSCDAttribute] =&gt; [MWSCDDefaultValue] =&gt; [MWSCDStatus] =&gt; 1 ) [4] =&gt; stdClass Object ( [MWSCID] =&gt; 2 [MWSCDID] =&gt; 5 [MWSCDName] =&gt; Merchant ID [MWSCDValue] =&gt; MerchantID [MWSCDRoot] =&gt; [MWSCDisParent] =&gt; [MWSCDTier] =&gt; [MWSCDParent] =&gt; [MWSCDNode] =&gt; 2 [MWSCDAttribute] =&gt; 2 [MWSCDDefaultValue] =&gt; XXXXXX-5348-XXXX-XXXX-4a553aa6fe94 [MWSCDStatus] =&gt; 1 ) [5] =&gt; stdClass Object ( [MWSCID] =&gt; 2 [MWSCDID] =&gt; 6 [MWSCDName] =&gt; Document Documentation [MWSCDValue] =&gt; DocDefinition [MWSCDRoot] =&gt; [MWSCDisParent] =&gt; [MWSCDTier] =&gt; [MWSCDParent] =&gt; [MWSCDNode] =&gt; 2 [MWSCDAttribute] =&gt; 2 [MWSCDDefaultValue] =&gt; XXXXXXXX-XXXX-4463-9EB0-36B992664AE4 [MWSCDStatus] =&gt; 1 ) [6] =&gt; stdClass Object ( [MWSCID] =&gt; 2 [MWSCDID] =&gt; 7 [MWSCDName] =&gt; Detail [MWSCDValue] =&gt; Dtl [MWSCDRoot] =&gt; [MWSCDisParent] =&gt; 3 [MWSCDTier] =&gt; [MWSCDParent] =&gt; 1 [MWSCDNode] =&gt; [MWSCDAttribute] =&gt; [MWSCDDefaultValue] =&gt; [MWSCDStatus] =&gt; 1 ) [7] =&gt; stdClass Object ( [MWSCID] =&gt; 2 [MWSCDID] =&gt; 8 [MWSCDName] =&gt; Customer ID [MWSCDValue] =&gt; CustID [MWSCDRoot] =&gt; [MWSCDisParent] =&gt; [MWSCDTier] =&gt; [MWSCDParent] =&gt; [MWSCDNode] =&gt; 3 [MWSCDAttribute] =&gt; 3 [MWSCDDefaultValue] =&gt; XXXX-PP [MWSCDStatus] =&gt; 1 ) ) </code></pre> <p>*NOTE: MWSCDParent Column: Tells which parent this node belongs to</p> <p>MWSCDisParent Column: Tells if this node is a parent</p> <p>MWSCDValue Column: Will be the Node name</p> <p>MWSCDAttribute column will indicate the record is an attribute for the MWSCDNode</p> <p>I am trying to make it to produce and XML with this output:</p> <pre><code>&lt;Message UserID="TestUser" Password="TestPassword"&gt; &lt;Document MerchantID="XXXXXXXX-5348-XXXX-XXXX-4a553aa6fe94" DocDefinition="XXXXXXXX-XXXX-4463-9EB0-36B992664AE4"&gt; &lt;Dtl CustID="XXXX-PP" RefID="&gt;&lt;/Dtl&gt; &lt;/Document&gt; &lt;/Message&gt; </code></pre> <p>I have tried:</p> <pre><code>$res_detail = $this-&gt;merchantwsconfigdetail_model-&gt;get_ws_detail(2); $res_user = $this-&gt;merchantwslogin_model-&gt;user_list(' WHERE MWSCID='.$res_detail[0]-&gt;MWSCID,'',''); $xmlDoc = new DOMDocument; $xmlDoc-&gt;formatOutput = true; for($a=0;$a&lt;count($res_detail);$a++): { if($res_detail[$a]-&gt;MWSCDRoot == "1"): { $root = $xmlDoc-&gt;appendChild($xmlDoc-&gt;createElement($res_detail[$a]-&gt;MWSCDValue)); $out_xml = simplexml_import_dom($xmlDoc); for($b=0;$b&lt;count($res_detail);$b++): { if(!empty($res_detail[$b]-&gt;MWSCDAttribute) &amp;&amp; $res_detail[$b]-&gt;MWSCDNode == "1"): { $val = ""; if(!empty($res_detail[$b]-&gt;MWSCDDefinedValue)): { if($res_detail[$b]-&gt;MWSCDDefinedValue == "5"): //Username { $val = $res_user[0]-&gt;MWSLUsername; } elseif($res_detail[$b]-&gt;MWSCDDefinedValue == "6"): { $val = $res_user[0]-&gt;MWSLPassword; } endif; $attr[$b] = simplexml_import_dom($root); $attr[$b]-&gt;addAttribute($res_detail[$b]-&gt;MWSCDValue,$val); } endif; } endif; } endfor; } endif; } endfor; $ctr = 0; for($a=0;$a&lt;count($res_detail);$a++): { if($res_detail[$a]-&gt;MWSCDRoot &lt;&gt; "1" &amp;&amp; !empty($res_detail[$a]-&gt;MWSCDParent)): { $node[$ctr] = simplexml_import_dom($root); $node[$ctr]-&gt;addChild($res_detail[$a]-&gt;MWSCDValue)-&gt;addAttribute('test','tex'); $ctr++; } } </code></pre> <p>I am getting:</p> <pre><code>&lt;message password="Password" userid="TestUser"&gt; &lt;document test="tex"&gt; &lt;dtl test="tex"&gt;&lt;/dtl&gt; &lt;/document&gt; &lt;/message&gt; </code></pre> <p>which is ok, but the problem arise is when the there is more than 1 in MWSCDParent which should look something like this:</p> <pre><code>&lt;a&gt; &lt;b attr="attrib_val1"&gt; &lt;d attr="attrib_val1&gt; *it goes deeper &lt;e attr="attrib_val1&gt;*here is the problem!? and the attribute is being inherited as well &lt;/e&gt; &lt;/d&gt; &lt;/b&gt; &lt;c&gt; *problem is here. I cant pinpoint it to be a child as well &lt;/c&gt; &lt;/a&gt; </code></pre> <p>I have set the MWSCDParent earlier tocheck if it will change corresponding to the value in the database, but it seems that</p> <pre><code>$node[$ctr] = simplexml_import_dom($root); $node[$ctr]-&gt;addChild($res_detail[$a]-&gt;MWSCDValue)-&gt;addAttribute('test','tex'); $ctr++; </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload