簡體   English   中英

如何縮進PHP while循環中的XML輸出?

[英]How to indent XML output from PHP while loop?

感謝您提供有關使用“ DOMDocument”方法,創建元素然后添加child的建議,這些內容對於我所追求的效果很好。

這是我想出的,它似乎可以用於返回一條記錄,但是,如果數據庫中有多條記錄(wp_hud_display.id)會破壞XML,則“ foreach”是否可以在while循環中工作? 以及如何將其添加到我的代碼中?

`

<?php    
    // create a dom document with encoding utf8 
    $domtree = new DOMDocument('1.0', 'UTF-8');

//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "root";
$config['mysql_pass'] = "";
$config['db_name']    = "tasmanhud";

//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");

    //select all items in table
    $sql = "SELECT
      wp_hud_display.id,
      wp_hud_display.name,
      wp_hud_display.resolution,
      wp_hud_display.user_id,
      wp_hud_display.instrument_id_1,
      wp_hud_display.x_loc_1,
      wp_hud_display.y_loc_1,
      wp_hud_display.layer_1,
      wp_hud_display.instrument_id_2,
      wp_hud_display.x_loc_2,
      wp_hud_display.y_loc_2,
      wp_hud_display.layer_2,
      wp_hud_display.instrument_id_3,
      wp_hud_display.x_loc_3,
      wp_hud_display.y_loc_3,
      wp_hud_display.layer_3,
      wp_hud_display.instrument_id_4,
      wp_hud_display.x_loc_4,
      wp_hud_display.y_loc_4,
      wp_hud_display.layer_4,
      wp_hud_instrument.inst_id AS inst1_id,
      wp_hud_instrument.name AS inst1_name,
      wp_hud_instrument.image_file AS inst1_image,
      wp_hud_instrument.font_type AS inst1_ft,
      wp_hud_instrument.font_size AS inst1_fs,
      wp_hud_instrument_1.inst_id AS inst2_id,
      wp_hud_instrument_1.name AS inst2_name,
      wp_hud_instrument_1.image_file AS inst2_image,
      wp_hud_instrument_1.font_type AS inst2_ft,
      wp_hud_instrument_1.font_size AS inst2_fs,
      wp_hud_instrument_2.inst_id AS inst3_id,
      wp_hud_instrument_2.name AS inst3_name,
      wp_hud_instrument_2.image_file AS inst3_image,
      wp_hud_instrument_2.font_type AS inst3_ft,
      wp_hud_instrument_2.font_size AS inst3_fs,
      wp_hud_instrument_3.inst_id AS inst4_id,
      wp_hud_instrument_3.name AS inst4_name,
      wp_hud_instrument_3.image_file AS inst4_image,
      wp_hud_instrument_3.font_type AS inst4_ft,
      wp_hud_instrument_3.font_size AS inst4_fs
    FROM wp_hud_display
      LEFT JOIN wp_hud_instrument
        ON wp_hud_display.instrument_id_1 = wp_hud_instrument.inst_id
      LEFT JOIN wp_hud_instrument wp_hud_instrument_1
        ON wp_hud_display.instrument_id_2 = wp_hud_instrument_1.inst_id
      LEFT JOIN wp_hud_instrument wp_hud_instrument_2
        ON wp_hud_display.instrument_id_3 = wp_hud_instrument_2.inst_id
      LEFT JOIN wp_hud_instrument wp_hud_instrument_3
        ON wp_hud_display.instrument_id_4 = wp_hud_instrument_3.inst_id
    WHERE wp_hud_display.user_id = 1
    GROUP BY wp_hud_display.id";

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

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

$hud_display = $domtree->createElement("hud_display");
$hud_display = $hud_display_group->appendChild($hud_display);

$instrument_id_1 = $domtree->createElement("instrument_id_1");
$instrument_id_1 = $hud_display->appendChild($instrument_id_1);

$instrument_id_2 = $domtree->createElement("instrument_id_2");
$instrument_id_2 = $hud_display->appendChild($instrument_id_2);

$instrument_id_3 = $domtree->createElement("instrument_id_3");
$instrument_id_3 = $hud_display->appendChild($instrument_id_3);

$instrument_id_4 = $domtree->createElement("instrument_id_4");
$instrument_id_4 = $hud_display->appendChild($instrument_id_4);

    $result = mysql_query($sql);
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

    if(mysql_num_rows($result)>0)
    {
       while($result_array = mysql_fetch_assoc($result))
       {

    /* you should enclose the following two lines in a cicle */

$hud_display->appendChild($domtree->createElement('id',$result_array['id']));
$hud_display->appendChild($domtree->createElement('name',$result_array['name']));
$hud_display->appendChild($domtree->createElement('resolution',$result_array['resolution']));
$hud_display->appendChild($domtree->createElement('user_id',$result_array['user_id']));

// Instrument 1
$hud_display->appendChild($domtree->createElement('instrument_id_1',$result_array['instrument_id_1']));
$instrument_id_1->appendChild($domtree->createElement('inst_id',$result_array['inst1_id']));
$instrument_id_1->appendChild($domtree->createElement('name',$result_array['inst1_name']));
$instrument_id_1->appendChild($domtree->createElement('image_name',$result_array['inst1_image']));
$instrument_id_1->appendChild($domtree->createElement('font_type',$result_array['inst1_ft']));  
$instrument_id_1->appendChild($domtree->createElement('font_size',$result_array['inst1_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_1',$result_array['x_loc_1']));
$hud_display->appendChild($domtree->createElement('y_loc_1',$result_array['y_loc_1']));
$hud_display->appendChild($domtree->createElement('layer_1',$result_array['layer_1']));
// Instrument 2


$hud_display->appendChild($domtree->createElement('instrument_id_2',$result_array['instrument_id_2']));
$instrument_id_2->appendChild($domtree->createElement('inst_id',$result_array['inst2_id']));
$instrument_id_2->appendChild($domtree->createElement('name',$result_array['inst2_name']));
$instrument_id_2->appendChild($domtree->createElement('image_name',$result_array['inst2_image']));
$instrument_id_2->appendChild($domtree->createElement('font_type',$result_array['inst2_ft']));  
$instrument_id_2->appendChild($domtree->createElement('font_size',$result_array['inst2_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_2',$result_array['x_loc_2']));
$hud_display->appendChild($domtree->createElement('y_loc_2',$result_array['y_loc_2']));
$hud_display->appendChild($domtree->createElement('layer_2',$result_array['layer_2']));
// Instrument 3 
$hud_display->appendChild($domtree->createElement('instrument_id_3',$result_array['instrument_id_3']));
$instrument_id_3->appendChild($domtree->createElement('inst_id',$result_array['inst3_id']));

    $instrument_id_3->appendChild($domtree->createElement('name',$result_array['inst3_name']));
    $instrument_id_3->appendChild($domtree->createElement('image_name',$result_array['inst3_image']));

$instrument_id_3->appendChild($domtree->createElement('font_type',$result_array['inst3_ft']));  
    $instrument_id_3->appendChild($domtree->createElement('font_size',$result_array['inst3_fs']));
    $hud_display->appendChild($domtree->createElement('x_loc_3',$result_array['x_loc_3']));
    $hud_display->appendChild($domtree->createElement('y_loc_3',$result_array['y_loc_3']));
    $hud_display->appendChild($domtree->createElement('layer_3',$result_array['layer_3']));
    // Instrument 4 
    $hud_display->appendChild($domtree->createElement('instrument_id_4',$result_array['instrument_id_4']));
    $instrument_id_4->appendChild($domtree->createElement('inst_id',$result_array['inst4_id']));
    $instrument_id_4->appendChild($domtree->createElement('name',$result_array['inst4_name']));
    $instrument_id_4->appendChild($domtree->createElement('image_name',$result_array['inst4_image']));

$instrument_id_4->appendChild($domtree->createElement('font_type',$result_array['inst4_ft']));  
    $instrument_id_4->appendChild($domtree->createElement('font_size',$result_array['inst4_fs']));
    $hud_display->appendChild($domtree->createElement('x_loc_4',$result_array['x_loc_4']));
    $hud_display->appendChild($domtree->createElement('y_loc_4',$result_array['y_loc_4']));
    $hud_display->appendChild($domtree->createElement('layer_4',$result_array['layer_4']));

        }
    }
// get the xml printed 

   echo $domtree->saveXML();



    //send the xml header to the browser

header ("Content-Type:text/xml");


    //output the XML data

echo $xml;
?>`

為什么不簡單地在連接的$xml變量中添加空間? 記住,您要做的只是編寫xml內容的文本並將其輸出到屏幕或文件。

XML還沒有被瀏覽器解釋為說刪除空格。 $xml字符串保存到文件后,將出現縮進。

  $xml.="<id>".$result_array['id']."</id>";
  $xml.="<name>".$result_array['name']."</name>";
  $xml.="<resolution>".$result_array['resolution']."</resolution>";
  $xml.="<user_id>".$result_array['user_id']."</user_id>";
  $xml.="<instrument_id_1>".$result_array['instrument_id_1']."</instrument_id_1>";
  $xml.="      <inst_id>".$result_array['inst1_id']."</inst_id>";
  $xml.="      <name>".$result_array['inst1_name']."</name>";
  $xml.="      <image_file>".$result_array['inst1_image']."</image_file>";
  $xml.="      <user_id>".$result_array['inst1_user_id']."</user_id>";
  $xml.="      <data_source>".$result_array['inst1_data_source']."</data_source>";
          ........................
  $xml.="<x_loc_1>".$result_array['x_loc_1']."</x_loc_1>";
  $xml.="<y_loc_1>".$result_array['y_loc_1']."</y_loc_1>";
  $xml.="<layer_1>".$result_array['layer_1']."</layer_1>";
  $xml.="<instrument_id_2>".$result_array['instrument_id_2']."</instrument_id_2>";
  $xml.="      <inst_id>".$result_array['inst2_id']."</inst_id>";
  $xml.="      <name>".$result_array['inst2_name']."</name>";
  $xml.="      <image_file>".$result_array['inst2_image']."</image_file>";
  $xml.="      <user_id>".$result_array['inst2_user_id']."</user_id>";
  $xml.="      <data_source>".$result_array['inst2_data_source']."</data_source>";
          .........................
  $xml.="      <font_size>".$result_array['inst2_fs']."</font_size>";
  $xml.="<x_loc_2>".$result_array['x_loc_2']."</x_loc_2>";
  $xml.="<y_loc_2>".$result_array['y_loc_2']."</y_loc_2>";
  $xml.="<layer_2>".$result_array['layer_2']."</layer_2>";

  //Screen output of XML data
  echo $xml;

  // Save output to file
  file_put_contents("File.xml", $xml);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM