首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >需要使用PHP编写XML -如何编写?

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

Stack Overflow用户
提问于 2010-07-09 21:26:28
回答 4查看 39.2K关注 0票数 23

我得到了这个基本的代码。

代码语言:javascript
复制
<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>

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

有人能给点建议吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-07-09 21:56:04

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

代码语言:javascript
复制
<?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();
?>
票数 47
EN

Stack Overflow用户

发布于 2010-07-09 21:39:47

我最喜欢的写XML文件的方法是XMLWriter - http://php.net/xmlwriter。它的功能非常强大,使用起来也很简单。

代码语言:javascript
复制
<?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(); 
    ?>

这段代码将生成以下XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<items>
 <main>
  <user_id>3</user_id>
  <msg_count>11</msg_count>
 </main>
 <msg category="test"/>
</items>
票数 12
EN

Stack Overflow用户

发布于 2010-07-09 21:28:48

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3212982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档