簡體   English   中英

PHP弄亂XML文件

[英]PHP Messing Up XML File

我有一個應用程序,其中將$ 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;
            }


        }
    }

}

您的$ _POST似乎為空或不包含您認為的結構。 由於XML中的祈禱的戰士正確地具有值為0,因此看起來$ firstName,$ lastName,$ request等是空變量。 您可以進行諸如print_r($ _ POST)之類的調試打印並查看其外觀嗎?

另外,正如@developerwjk在評論中提到的, <tagname />是完全有效的XML。 如果您嘗試使用正則表達式或XML解析器以外的任何其他東西解析XML結構,請立即停止並使用XML解析器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM