我有一个应用程序,我向PHP脚本发送$ POST,目的是将新项目添加到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;
}
}
}
}
答案 0 :(得分:0)
看起来你的$ _POST是空的,或者不包含你认为它的结构。由于prayer_warriors在XML中正确地具有值0,因此看起来$ firstName,$ lastName,$ request等是空变量。你可以做一个调试打印,比如print_r($ _ POST),看看它是什么样的吗?
此外,正如评论中提到的@developerwjk,<tagname />
是完全有效的XML。如果您尝试使用正则表达式或XML解析器以外的任何其他方法解析XML结构,请立即停止并使用XML解析器。