0

friends i managed to make following output from a php code
0 0 1 0 2 0 0

0 0 0 0 0 0 3

1 0 0 1 0 2 0

0 0 1 0 0 0 3

2 0 0 0 0 0 0

0 0 2 0 0 0 0

0 3 0 3 0 0 0

1==>3 3==>4 4==>7 7==>2 3==>6 1==>5

<html>
<?php
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

$result= array(array(0,0,1,0,2,0,0),array(0,0,0,0,0,0,3),array(1,0,0,1,0,2,0),
     array(0,0,1,0,0,0,3),array(2,0,0,0,0,0,0),array(0,0,2,0,0,0,0),
     array(0,3,0,3,0,0,0));
echo "<pre>";
 for($k = 0; $k < 7; $k++){
  for($j = 0; $j < 7; $j++){
                 echo $result[$k][$j],"\t";
       }
  echo "\n<br>";
 }
 $sum=0;
 for($i = 0; $i < 7; $i++){
  for($j = 0; $j < 7; $j++){
            $sum += $result[$i][$j];
  }  
 }
 
while( $sum > 0)
{
$i = 0;
 while($i < 7){
 $j=0;
 while($j<7)
 {
       hm: if($result[$i][$j]!=0)
        {  echo $i+1;
        print "==>";
     $result[$i][$j]=0;
     $result[$j][$i]=0;
     $i=$j;
     $j=0;
     echo $i+1;
     print "  ";
     goto hm;
     }
     $j++;
 }
 $i++;
 }
 $sum=0;
for($i = 0; $i < 7; $i++){
  for($j = 0; $j < 7; $j++){
            $sum += $result[$i][$j];
  }  
 }
 }

 
 
//..................................................................
?>
</html>

EXPECTED OUTPUT
<markers>
  <marker from="1" to="3"/>
  <marker from="3" to="4"/>
  <marker from="4" to="7"/>
  <marker from="7" to="2"/>
  <marker from="3" to="6"/>
  <marker from="1" to="5"/>
</markers>
  • Your question seemed pretty broaded (and unconcrete), according to your question and answer, you're concerned about creating XML with DOMDocument. That has been answered already and I made that visible via the duplicate close. – hakre Sep 28 '14 at 08:44

1 Answers1

0

<?php

header('Content-type: text/xml');
    /* create a dom document with encoding utf8 */
    $domtree = new DOMDocument('1.0', 'UTF-8');

    /* create the root element of the xml tree */
    $xmlRoot = $domtree->createElement("xml");
    /* append it to the document created */
    $xmlRoot = $domtree->appendChild($xmlRoot);

    $currentTrack = $domtree->createElement("track");
    $currentTrack = $xmlRoot->appendChild($currentTrack);

$result= array(array(0,0,1,0,2,0,0),array(0,0,0,0,0,0,3),array(1,0,0,1,0,2,0),
     array(0,0,1,0,0,0,3),array(2,0,0,0,0,0,0),array(0,0,2,0,0,0,0),
     array(0,3,0,3,0,0,0));
/*echo "<pre>";
 for($k = 0; $k < 7; $k++){
  for($j = 0; $j < 7; $j++){
                 echo $result[$k][$j],"\t";
       }
  echo "\n<br>";
 }*/
 $sum=0;
 for($i = 0; $i < 7; $i++){
  for($j = 0; $j < 7; $j++){
            $sum += $result[$i][$j];
  }  
 }
 
while( $sum > 0)
{
$i = 0;
 while($i < 7){
 $j=0;
 while($j<7)
 {
       hm: if($result[$i][$j]!=0)
        {  /* you should enclose the following two lines in a cicle */
    $currentTrack->appendChild($domtree->createElement('from',$i+1));
        $result[$i][$j]=0;
     $result[$j][$i]=0;
     $i=$j;
     /* you should enclose the following two lines in a cicle */
     $currentTrack->appendChild($domtree->createElement('to',$j+1));
              $j=0;
     goto hm;
     }
     $j++;
 }
 $i++;
 }
 $sum=0;
for($i = 0; $i < 7; $i++){
  for($j = 0; $j < 7; $j++){
            $sum += $result[$i][$j];
  }  
 }
 }

  
    /* get the xml printed */
    echo $domtree->saveXML();

 
//..................................................................
?>


output

<xml>
  <track>
    <from>1</from>
    <to>3</to>
    <from>3</from>
    <to>4</to>
    <from>4</from>
    <to>7</to>
    <from>7</from>
    <to>2</to>
    <from>3</from>
    <to>6</to>    
    <from>1</from>
    <to>5</to>
  </track>
</xml>