【问题标题】:How to make php to print xml for this program? [duplicate]如何让 php 为这个程序打印 xml? [复制]
【发布时间】:2014-11-22 09:21:36
【问题描述】:

朋友们,我设法从 php 代码中进行了以下输出
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>

预期输出
<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>

【问题讨论】:

  • 您的问题似乎很广泛(而且不具体),根据您的问题和回答,您担心使用 DOMDocument 创建 XML。这已经得到了回答,我通过重复关闭使其可见。

标签: php xml converter


【解决方案1】:

<?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();

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


输出

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2017-07-30
    相关资源
    最近更新 更多