【问题标题】:PHP Messing Up XML FilePHP 搞砸 XML 文件
【发布时间】:2014-07-18 04:34:56
【问题描述】:

我有一个应用程序,我在其中将 $POST 发送到 php 脚本,目的是向 XML 文件添加新项目。每隔一段时间,XML 就会添加一堆空白字段,导致无法正确解析。 XML 的结尾如下所示:

<item><first_name/><last_name/><title/><date/><anonymous/><prayer_warriors>0</prayer_warriors><location/><description/><iostoken/></item>

我使用的脚本如下:

<?php
$firstName = $_POST['firstName'];
 $lastName = $_POST['lastName'];
 $request = $_POST['request'];
 $anon = $_POST['anon'];
 $pubDate = $_POST['pubDate'];
 $loc = $_POST['loc'];
 $des = $_POST['des'];
 $iostoken = $_POST['iostoken'];
//This line will load the XML file
$xml = simplexml_load_file("http://Test.xml") or die("Not loaded!\n");
//print_r($xml);
//This line gets the channel element (from an array returned by the xpath method) 
$channel = $xml->xpath('//channel'); 
$channel = $channel[0];
//print_r($channel);
$person = $channel->addChild("item");
$person->addChild("first_name", $firstName);
$person->addChild("last_name", $lastName);
$person->addChild("title", $request);
$person->addChild("date", $pubDate);
$person->addChild("anonymous", $anon);
$person->addChild("prayer_warriors", "0");
$person->addChild("location", $loc);
$person->addChild("description", $des);
$person->addChild("iostoken", $iostoken);

//This next line will overwrite the original XML file with new data added
$xml->asXML("Test.xml");
?>

这个脚本有什么错误会导致空标签吗?

$_POST 来自我为此构建的应用程序发送的信息。基本上,我有名字、姓氏、设备令牌、祈祷请求、标题和描述以及日期的文本字段,它使用 PHP 获取这些字段并将它们放入 XML 文件中。 PHP 可以工作,因为我可以在这些字段中输入文本并编辑 XML,但每隔一段时间它会在最后添加它,我的应用程序将无法解析并崩溃。

我在应用程序中的解析器如下所示:

- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {

    NSArray *channels = [rootElement elementsForName:@"channel"];
    for (GDataXMLElement *channel in channels) {

        NSString *blogTitle = [channel valueForChild:@"title"];

        NSArray *items = [channel elementsForName:@"item"];
        for (GDataXMLElement *item in items) {

            NSString *firstName = [item valueForChild:@"first_name"];
            NSString *lastName = [item valueForChild:@"last_name"];
            NSString *articleDateString = [item valueForChild:@"date"];
            NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
            int daysToAdd = 7;
            NSDate *newDate1 = [articleDate dateByAddingTimeInterval:60*60*24*daysToAdd];
            NSString *anonymous = [item valueForChild:@"anonymous"];
            NSString *prayerRequest = [item valueForChild:@"title"];
            NSString *prayerWarriors = [item valueForChild:@"prayer_warriors"];
            NSString *location = [item valueForChild:@"location"];
            NSString *details = [item valueForChild:@"description"];
            NSString *devicestoken = [item valueForChild:@"iostoken"];
            NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
            [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
            [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
            NSString *dateofarticle = [dateFormatter stringFromDate:articleDate];
            NSString *expirationDate = [dateFormatter stringFromDate:newDate1];
            NSDate *today = [NSDate date];
            NSComparisonResult result = [today compare:articleDate];
            NSString *bodyoftext = [[[[[[@"<b><font size=5><div align=\"left\">" stringByAppendingString:prayerRequest] stringByAppendingString:@"</font></b><font size=3><p style=\"color:#989898\">"] stringByAppendingString:@"Expires " ] stringByAppendingString:expirationDate] stringByAppendingString:@"</div></p>"] stringByAppendingString:details];

            RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle firstName:firstName lastName:lastName articleDate:articleDate prayerRequest:prayerRequest anonymous:anonymous prayerWarriors:prayerWarriors location:location details:bodyoftext expiresDate:newDate1 iostoken:devicestoken] autorelease];
            switch (result)
            {
                case NSOrderedAscending:
                    NSLog(@"Future Date");
                    break;
                case NSOrderedDescending:


                    [entries addObject:entry];
                    NSLog(@"Earlier Date");
                    break;
                case NSOrderedSame:


                    [entries addObject:entry];
                    NSLog(@"Today/Null Date Passed"); //Not sure why This is case when null/wrong date is passed
                    break;
                default:
                    NSLog(@"Error Comparing Dates");
                    break;
            }


        }
    }

}

【问题讨论】:

  • &lt;first_name/&gt; 这样的空标签不会使XML“无法解析”。当您没有 innerText 时,这很正常。大多数解析器更喜欢&lt;first_name/&gt; 而不是&lt;first_name&gt;&lt;/first_name&gt;

标签: php ios xml


【解决方案1】:

您的 $_POST 似乎是空的或不包含您认为的结构。由于pray_warriors 在XML 中正确地具有0 值,因此看起来$firstName、$lastName、$request 等是空变量。你能做一个调试打印,比如 print_r($_POST) 看看它是什么样子的吗?

另外,正如@developerwjk 在评论中提到的,&lt;tagname /&gt; 是完全有效的 XML。如果您尝试使用正则表达式或任何非 XML 解析器来解析 XML 结构,请立即停止并使用 XML 解析器。

【讨论】:

  • $_POST 来自我为此构建的应用程序发送的信息。基本上,我有名字、姓氏、设备令牌、祈祷请求、标题和描述以及日期的文本字段,它使用 PHP 获取这些字段并将它们放入 XML 文件中。 PHP 可以工作,因为我可以在这些字段中输入文本并编辑 XML,但每隔一段时间它会在最后添加它,我的应用程序将无法解析并崩溃。
  • 如果$person-&gt;addChild("first_name", $firstName); 正在创建一个看起来像&lt;first_name /&gt; 的标签,那么$firstName 要么为空,要么为空字符串。
  • 但是其他尝试使用 PHP 添加到它的尝试效果很好,并且应用程序在所有字段都被占用之前不允许发送任何内容。
  • 您可以随时在脚本顶部添加if(!isset($_POST['firstName'])) die() 之类的内容。
  • 应用程序失败的原因似乎是因为它试图将每个项目的代码中的日期从 NSString 转换为 NSDate,但由于日期标签为空,它会崩溃。我的问题是为什么 XML 会得到这些空标签?
猜你喜欢
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 2014-04-28
  • 2012-06-08
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
相关资源
最近更新 更多