我在网上可以找到的所有这些例子都只涉及在文档根目录中向XML文件中添加内容,但我真的需要做更深入的事情.
我的XML文件很简单,我有:
<?xml v1 etc>
<channel>
<screenshots>
<item>
<title>Image Title</title>
<link>www.link.com/image.jpg</link>
</item>
</screenshots>
</channel>
Run Code Online (Sandbox Code Playgroud)
我想要做的就是添加新的"item"元素,每个元素都带有标题和链接.我知道我需要使用PHP DOM,但我对如何编码它感到困惑,因此它在"屏幕截图"中添加数据而不是覆盖整个文档.我怀疑我可能也需要使用XPath,但我不知道怎么做!
我从在线示例拼凑而来的代码看起来像这样(但我确定这是错的)
$newshottitle = "My new screenshot";
$newshotlink = "http://www.image.com/image.jpg";
$dom = newDomDocument;
$dom->formatOutput = true;
$dom->load("../xml/screenshots.xml");
$dom->getElementsByTagName("screenshots");
$t = $dom->createElement("item");
$t = $dom->createElement("title");
$t->appendChild($dom->createTextNode("$newshottitle"));
$l = $dom->createElement("link");
$l->appendChild($dom->createTextNode("$newshotlink"));
$dom->save("../xml/screenshots.xml");
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中遇到了一些NSString的困难.基本上,我有一个名为o1string的NSString,它包含值"602".我想在UIAlertView中输出这个以及其他一些文本.
votedmessage = [ NSString stringWithFormat:@"The current standings are as follows:\n\n%@: %@ votes", b1title, o1string ];
UIAlertView *votedAlert = [[UIAlertView alloc] initWithTitle:@"Thank you for voting" message:votedmessage delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
Run Code Online (Sandbox Code Playgroud)
我已经使用了NSLog并验证了NSString中的值肯定是602,而消息中使用的另一个变量(b1title)自己输出正常.当我将o1votes变量添加到警报消息时,我无法弄清楚为什么应用程序崩溃了,是否与在NSString中只保留一个数字的冲突有关?
这是o1string的设置方式.它肯定包含"602",从XML文件中获取.
o1string = [[options objectAtIndex:3] objectForKey: @"votes"];
o1string = [o1string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
o1string = [o1string stringByReplacingOccurrencesOfString:@" " withString:@""];
Run Code Online (Sandbox Code Playgroud)