需要使用 PHP 编写 XML - 如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3212982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:01:51  来源:igfitidea点击:

Need to write XML using PHP - how?

phpxml

提问by GaxZE

I've got this basic code.

我有这个基本代码。

<chart lowerLimit='0' upperLimit='100' caption='Revenue' subcaption='US $ (1,000s)' numberPrefix='$' numberSuffix='K' showValue='1' >
   <colorRange>
      <color minValue='0' maxValue='50' color='A6A6A6'/>
      <color minValue='50' maxValue='75' color='CCCCCC'/> 
      <color minValue='75' maxValue='100' color='E1E1E1'/> 
   </colorRange> 
   <value>78.9</value>
   <target>80</target>
</chart>

it's used from fusionwidgets and there's no documentation on how to write this in PHP.

它是从 fusionwidgets 中使用的,并且没有关于如何在 PHP 中编写它的文档。

can anybody advise?

有人可以建议吗?

回答by Māris Kise?ovs

There is complete example with php.net/XMLWriterto produce exactly the same XML output like you posted.

php.net/XMLWriter有一个完整的示例,可以生成与您发布的完全相同的 XML 输出。

<?php
$writer = new XMLWriter();  
$writer->openURI('php://output');  
$writer->startDocument('1.0','UTF-8');  
$writer->setIndent(4);   
$writer->startElement('chart');  
   $writer->writeAttribute('lowerLimit', '0');  
   $writer->writeAttribute('upperLimit', '100');  
   $writer->writeAttribute('caption', 'Revenue');  
   $writer->writeAttribute('subcaption', 'US $ (1,000s)');  
   $writer->writeAttribute('numberPrefix', '$');  
   $writer->writeAttribute('numberSuffix', 'K');  
   $writer->writeAttribute('showValue', '1');  
   $writer->startElement('colorRange');  
      $writer->startElement('color');  
         $writer->writeAttribute('minValue', '0');  
         $writer->writeAttribute('maxValue', '50'); 
         $writer->writeAttribute('color', 'A6A6A6'); 
      $writer->endElement();    
      $writer->startElement('color');  
         $writer->writeAttribute('minValue', '50');  
         $writer->writeAttribute('maxValue', '75'); 
         $writer->writeAttribute('color', 'CCCCCC'); 
      $writer->endElement();  
      $writer->startElement('color');  
         $writer->writeAttribute('minValue', '75');  
         $writer->writeAttribute('maxValue', '100'); 
         $writer->writeAttribute('color', 'E1E1E1'); 
      $writer->endElement();  
   $writer->endElement();  
   $writer->writeElement('value','78.9');  
   $writer->writeElement('target','78.9');  
$writer->endElement();  
$writer->endDocument();   
$writer->flush();
?>

回答by Māris Kise?ovs

My favorite way to write XML files is XMLWriter - http://php.net/xmlwriter. It's very powerfull and simple to use.

我最喜欢的编写 XML 文件的方法是 XMLWriter - http://php.net/xmlwriter。它非常强大且易于使用。

<?php
           $writer = new XMLWriter();  
           $writer->openURI('php://output');   
           $writer->startDocument('1.0','UTF-8');   
           $writer->setIndent(4);   
           $writer->startElement('items');  
           $writer->startElement("main");  
           $writer->writeElement('user_id', 3);  
           $writer->writeElement('msg_count', 11);  
           $writer->endElement();   
           $writer->startElement("msg");  
           $writer->writeAttribute('category', 'test');  
           $writer->endElement();     
           $writer->endElement();   
           $writer->endDocument();   
           $writer->flush(); 
    ?>

And that piece of code will produce the following XML:

这段代码将生成以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<items>
 <main>
  <user_id>3</user_id>
  <msg_count>11</msg_count>
 </main>
 <msg category="test"/>
</items>

回答by Gordon

See

Personally, I prefer DOMfor working with XML.

就个人而言,我更喜欢使用DOM来处理 XML。

You might want to clearify your question. Right now, it's hard to tell what your problem is.

您可能想澄清您的问题。现在,很难说你的问题是什么。

回答by Daniele Orlando

With FluidXMLyou can generate your XML in this way.

使用FluidXML,您可以通过这种方式生成 XML。

$chart = fluidxml('chart');

$chart->attr('lowerLimit', 0)
      ->attr('upperLimit', 100)
      ->attr(...)
      ->add('colorRange')
      ->add('value', 78.9)
      ->add('target', 80)
      ->query('//colorRange')
          ->add('color', ['minValue' => 0, 'maxValue' => 50, ...])
          ->add('color', ['minValue' => 50, 'maxValue' => 75, ...])
          ->add('color', ['minValue' => 75, 'maxValue' => 100, ...]);

https://github.com/servo-php/fluidxml

https://github.com/servo-php/fluidxml

回答by Gal Zilberman

As mentioned before, setIndent sets indentation on and should be used like this:

如前所述, setIndent 设置缩进并且应该像这样使用:

$writer->setIndent(true);

if you want to set the indentation size to a different one than the default (2 spaces), you can use setIndentString:

如果要将缩进大小设置为与默认值不同的大小(2 个空格),可以使用 setIndentString:

$writer->setIndentString("    ");

回答by berkes

SimpleXML, wich is built into PHP is the most simple solution for writing (and parsing) XML. http://php.net/manual/en/book.simplexml.php

内置于 PHP 中的 SimpleXML 是编写(和解析)XML 的最简单的解决方案。http://php.net/manual/en/book.simplexml.php