douben6670 2015-08-26 19:17
浏览 35
已采纳

使用PHP更改XML的结构

I need to take this text file http://gis.fcd.maricopa.gov/apps/forecastzone/wxlabels.txt and using php export an xml file. I have this current code which exports the xml but I need a different format

<?php

date_default_timezone_set("MST"); //or whatever your default timezone is.
header("Content-Type: text/xml"); 

$fp = fopen('http://gis.fcd.maricopa.gov/apps/forecastzone/wxlabels.txt', 'r');

$xml = new XMLWriter;
$xml->openURI('php://output');
$xml->startDocument(); 
$xml->setIndent(true); // makes output cleaner

$xml->startElement('messages'); 

while ($line = fgetcsv($fp)) {

   $xml->startElement('zones');
   $xml->writeElement('name', $line[0]);
   $xml->writeElement('times', $line[1]);
   $xml->endElement();
}

$xml->endElement();

?>

It exports to http://alert.fcd.maricopa.gov/alert/Google/v3/php/msp.php and looks like this:

<messages>
<zones>
<name>Gila Bend</name>
<times>2:00pm-5:00pm</times>
</zones>
</messages>

I need it to look like this:

<messages>
<zones name="Gila Bend" times="2:00pm-5:00pm"</zones>
</messages>

What do I need to add/edit in my php file?

  • 写回答

1条回答 默认 最新

  • dozpv84422 2015-08-26 22:59
    关注
    <?php
    
    date_default_timezone_set("MST"); //or whatever your default timezone is.
    header("Content-Type: text/xml"); 
    
    $fp = fopen('http://gis.fcd.maricopa.gov/apps/forecastzone/wxlabels.txt', 'r');
    
    $xml = new XMLWriter;
    $xml->openURI('php://output');
    $xml->startDocument(); 
    $xml->setIndent(true); // makes output cleaner
    
    $xml->startElement('messages'); 
    
    while ($line = fgetcsv($fp)) {
    
       $xml->startElement('zones');
       $xml->writeAttribute('name', $line[0]);
       $xml->writeAttribute('times', $line[1]);
       $xml->endElement();
    }
    
    $xml->endElement();
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序