php
  • xml
  • 2012-01-06 74 views -3 likes 
    -3

    可能重複:
    format xml stringXML源是凌亂

    我生成的XML頁面像這樣:

      header('Content-Type: text/html');    
    
          $xmlpage = '<?xml version="1.0" charset="utf-8"?>'; 
    
          $xmlpage .= '<conv>'; 
          $xmlpage .= '<at>6 January 2012 12:00</at>'; 
          $xmlpage .= '<rate>1.56317</rate>'; 
    
          $xmlpage .= '<from>'; 
          $xmlpage .= '<code>'.$from.'</code>'; 
          $xmlpage .= '<curr>Pound Sterling</curr>'; 
          $xmlpage .= '<loc>UK</loc>'; 
          $xmlpage .= '<amnt>'.$amnt.'</amnt>'; 
          $xmlpage .= '</from>'; 
    
          $xmlpage .= '</conv>'; 
    
          echo $xmlpage; 
    

    當查看網頁的源,它看起來很糟糕:

    <?xml version="1.0" charset="utf-8"?><conv><at>6 January 2012 12:00</at><rate>1.56317</rate><from><code>USD</code><curr>Pound Sterling</curr><loc>UK</loc><amnt>23</amnt></from><to><code>GBP</code><curr>United States Dollar</curr><loc>USA</loc><amnt>14.73</amnt></to></conv> 
    

    我該怎麼做才能正確格式化和縮進?

    +0

    你爲什麼要發送內容類型html的XML? – 2012-01-06 19:55:35

    +0

    忽略,僅僅是一個輸入錯誤 – tctc91 2012-01-07 12:23:06

    回答

    5

    使用\ r \ n或僅\ n字符添加換行符。您需要將您的字符串放在雙引號(「」)中,以便它能夠正常工作,所以或者用單引號(')替換字符串中的雙引號,轉義雙引號(\「),添加。」\ r \ n」個作爲斷行或使用HEREDOC

    與像內置SimpleXML將阻止這些排序和許多其他類型的問題一個XML生成建立你的XML和通常比用手工構建它要容易得多字符串。

    +0

    +1表示heredoc。 – 2012-01-06 16:51:47

    +0

    +1表示heredoc。 – ThinkingMonkey 2012-01-06 17:50:32

    1

    $xmlpage後添加\n,你應該能夠在echo後正常查看。

    例如

     $xmlpage = "<?xml version="1.0" charset="utf-8"?>\n"; 
    
         $xmlpage .= "<conv>\n"; 
         $xmlpage .= "<at>6 January 2012 12:00</at>\n"; 
         $xmlpage .= "<rate>1.56317</rate>\n"; 
    
    +0

    單引號字符串加'\ n'不能很好地一起玩...... – 2012-01-06 17:27:07

    +0

    基本的PHP語法:http://www.php.net/manual/en/language.types.string.php#language.types。 string.syntax.single – 2012-01-06 17:43:04

    +0

    哈哈,我不知道。我認爲還有更多的東西。不過謝謝。 – ThinkingMonkey 2012-01-06 17:48:28

    2

    ,你可以:

    • 加入空格字符到您的字符串(\n\t)自己做。
    • 輸出與HEREDOC
    • 所有的XML,您可以創建,甚至產生DOMDocument和使用saveXML()

    前兩個是快速和骯髒(定界符的更好)。後者更健壯,但代碼更多。

    +0

    Downvoter關心分享? – 2012-01-06 18:06:03

    1

    使用HEREDOC。它會更容易比重複字符串連接閱讀,可以讓標籤/多線,並執行變量代換爲您提供:

    $xmlpage = <<<EOL 
    <?xml version="1.0" charset="utf-8"?> 
    <conv> 
        <at>6 January 2012 12:00</at> 
        <rate>1.56317</rate> 
        <from> 
         <code>$from</code> 
         <curr>Pound Sterling</curr> 
         <loc>UK</loc> 
         <amnt>$amnt</amnt> 
        </from> 
    </conv> 
    EOL; 
    
    +0

    對於heredoc來說+1是個例子。 – 2012-01-06 16:57:57

    -1

    最簡單的方法是適當的空格添加到字符串的開頭,換行符結束。

     $xmlpage = '<?xml version="1.0" charset="utf-8"?>'; 
    
         $xmlpage .= '<conv>' . "\n"; 
         $xmlpage .= "\t" . '<at>6 January 2012 12:00</at>' . "\n"; 
         $xmlpage .= "\t" . '<rate>1.56317</rate>' . "\n"; 
    
         $xmlpage .= '<from>' . "\n"; 
         $xmlpage .= "\t" . '<code>'.$from.'</code>' . "\n"; 
         $xmlpage .= "\t" . '<curr>Pound Sterling</curr>' . "\n"; 
         $xmlpage .= "\t" . '<loc>UK</loc>' . "\n"; 
         $xmlpage .= "\t" . '<amnt>'.$amnt.'</amnt>' . "\n"; 
         $xmlpage .= '</from>' . "\n"; 
    
         $xmlpage .= '</conv>'; 
    

    沿着這些線或根據您所需的輸出的東西。

    -2

    這裏是我的美化功能,輸出格式。您可以修改它以滿足您的需求。

    function prettifyXML($xml) 
    { 
        // Break our XML up into sections of newlines. 
        $xml = preg_replace('/(<[^\/][^>]*?[^\/]>)/', "\n" . '\1', $xml); 
    
        $xml = preg_replace('/(<\/[^\/>]*>|<[^\/>]*?\/>)/', '\1' . "\n", $xml); 
        $xml = str_replace("\n\n", "\n", $xml); 
    
        $xml_chunks = explode("\n", $xml); 
    
        $indent_depth = 0; 
        $open_tag_regex = '/<[^\/\?][^>]*>/'; 
        $close_tag_regex = '/(<\/[^>]*>|<[^>]*\/>)/'; 
    
        // Fix the indenting. 
        foreach ($xml_chunks as $index => $xml_chunk) 
        { 
         $close_tag_count = preg_match($close_tag_regex, $xml_chunk); 
    
         $open_tag_count = preg_match($open_tag_regex, $xml_chunk); 
    
         if ($open_tag_count >= $close_tag_count) 
         { 
          $temp_indent_depth = $indent_depth; 
         } 
         else 
         { 
          $temp_indent_depth = $indent_depth - $close_tag_count; 
         } 
    
         $xml_chunks[ $index ] = str_repeat("\t", $temp_indent_depth) . $xml_chunk; 
    
         $indent_depth += $open_tag_count - $close_tag_count; 
    
        } 
    
        $xml = implode("\n", $xml_chunks); 
    
        // Add tokens for attributes and values. 
        $attribute_regex = '/([\w:]+\="[^"]*")/'; 
        $value_regex = '/>([^<]*)</'; 
    
        $value_span_token = '##@@##@@'; 
        $attribute_span_token = '@@##@@##'; 
        $span_close_token = '#@#@#@#@'; 
    
        $xml = preg_replace($value_regex, '>' . $value_span_token . '\1' . $span_close_token . '<', $xml); 
        $xml = preg_replace($attribute_regex, $attribute_span_token . '\1' .$span_close_token, $xml); 
        $xml = htmlentities($xml); 
    
        // Replace the tokens that we added previously with their HTML counterparts. 
        $xml = str_replace($value_span_token, '<span class="value">', $xml); 
        $xml = str_replace($attribute_span_token, '<span class="attribute">', $xml); 
        $xml = str_replace($span_close_token, '</span>', $xml); 
    
        return $xml; 
    } 
    

    雖然處理邊緣案例的效率相對較高,但它效率並不高,因爲它僅用於查看日誌。

    +0

    XML + regex = [BAD](http://stackoverflow.com/a/1732454/118068) – 2012-01-06 17:28:00

    +0

    Marc,我不同意--XML是一個常規的數據結構,非常適合被正則表達式解析。在解析大型XML文檔時,有時使用正則表達式會更快,而且它嘗試解析整個文檔時的內存肯定更少。 – 2012-01-06 17:31:08

    +0

    在一個完美的世界裏,是的。但是非常頻繁地發生了損壞的數據,並且對破壞性xml的正則表達式會使其更糟糕。總是防守編程。 – 2012-01-06 17:32:02

    1

    使用樣式表和XML查看器來查看它。

    +0

    有點矯枉過正吧?即使在純文本編輯器中,這樣一個簡單的XML也應該很容易查看。 – dtech 2012-01-06 16:56:31

    +0

    這是一個最佳實踐,不管XML的複雜性(或缺乏)。這並不是特別困難 - 矯枉過正提出了一個輕量級工作的重量級工具,而XSLT不是一個重量級工具,也不是XML瀏覽器最近很難出現,所以不,我認爲這不構成矯枉過正。 – 2012-01-06 23:35:52

    相關問題