在我问这个问题之前我已经在这里搜索过..我知道这里某处有答案..但我找不到..
我希望能够在我的 xml 文件中通过 id 编辑子节点。我尝试了很多脚本,但我在这方面遇到了非常糟糕的时间。
请在 edit.php 中查看这一行:
echo "the problem is in the next line";
这是服务器读取的最后一行..
谢谢你的帮助。
事件.xml
<?xml version="1.0" encoding="UTF-8" ?>
<events>
<record>
<id>1</id>
<event>a</event>
<eventDate>a</eventDate>
<desc>a</desc>
</record>
<record>
<id>2</id>
<event>b</event>
<eventDate>b</eventDate>
<desc>b</desc>
</record>
</events>
edit.php
header("Content-type: text/html; charset=utf-8");
$record = array(
'id' => $_POST['id'],
'event' => $_POST['event'],
'eventDate' => $_POST['eventDate'],
'desc' => $_POST['desc'],
);
$id = $record["id"];
$dom = new DOMDocument;
$dom->load('events.xml');
$xpath = new DOMXPath($dom);
$query = sprintf('/events/record[./id = "%d"]', $id);
foreach($xpath->query($query) as $record) {
$eventN = $record->parentNode->getElementsByTagName("event");
echo "the problem is in the next line";
$eventN->item(0)->nodeValue = $record["event"];
$dateN = $record->parentNode->getElementsByTagName("eventDate");
$dateN->item(0)->nodeValue = $record["eventDate"];
$descN = $record->parentNode->getElementsByTagName("desc");
$descN->item(0)->nodeValue = $record["desc"];
}
$dom->save("events.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
已编辑:工作 edit.php 但不是动态的
<?php
header("Content-type: text/html; charset=utf-8");
$record = array(
'id' => $_POST['id'],
'event' => $_POST['event'],
'eventDate' => $_POST['eventDate'],
'desc' => $_POST['desc'],
);
$id = $record["id"];
$dom = new DOMDocument;
$dom->load('events.xml');
$xpath = new DOMXPath($dom);
$query = sprintf('/events/record[./id = "%d"]', $id);
foreach($xpath->query($query) as $record) {
$eventN = $record->parentNode->getElementsByTagName("event");
echo "i change it to string text and it's works. ";
$eventN->item(0)->nodeValue = 'text';
$dateN = $record->parentNode->getElementsByTagName("eventDate");
$dateN->item(0)->nodeValue = 'text';
$descN = $record->parentNode->getElementsByTagName("desc");
$descN->item(0)->nodeValue = 'text';
}
$dom->save("events.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
最佳答案
好的,问题是您的 $record 数组从一开始就在 foreach 中被覆盖了。
要么更改 as $record,要么更改保存 $_POST 数据的数组的名称。
旁注,您正在遍历元素,因此没有理由到达父节点。使用
$record->getElementsByTagName("event");
关于php - 如何用php编辑xml子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4669240/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
A/ctohttp://wiki.nginx.org/CoreModule#usermaster进程曾经以root用户运行,是否可以以不同的用户运行nginxmaster进程? 最佳答案 只需以非root身份运行init脚本(即/etc/init.d/nginxstart),就可以用不同的用户运行nginxmaster进程。如果这真的是你想要做的,你将需要确保日志和pid目录(通常是/var/log/nginx&/var/run/nginx.pid)对该用户是可写的,并且您所有的listen调用都是针对大于1024的端口(因为绑定(
我正在尝试解决http://projecteuler.net/problem=1.我想创建一个方法,它接受一个整数,然后创建一个包含它前面的所有整数的数组,并将整数本身作为数组中的值。以下是我目前所拥有的。代码不起作用。defmake_array(num)numbers=Array.newnumcount=1numbers.eachdo|number|numbers 最佳答案 (1..num).to_a是您在Ruby中需要做的全部。1..num将创建一个Range对象,以1开始并以任意值num结束是。Range对象有to_a方法通过
我有这样的HTML代码:Label1Value1Label2Value2...我的代码不起作用。doc.css("first").eachdo|item|label=item.css("dt")value=item.css("dd")end显示所有首先标记,然后标记标签,我需要“标签:值” 最佳答案 首先,您的HTML应该有和中的元素:Label1Value1Label2Value2...但这不会改变您解析它的方式。你想找到s并遍历它们,然后在每个你可以使用next_element得到;像这样:doc=Nokogiri::HTML(
我使用“newapp_name”创建了一个新的Rails应用程序,我正在尝试编辑.gitignore文件,但在我的应用程序文件夹中找不到它。我在哪里可以找到它?我安装了Git。 最佳答案 .gitignore位于项目的root中,而不是app子目录中。首先打开终端并进入您的目录。您需要使用ls-a来显示stash文件。然后使用打开.gitignore 关于ruby-on-rails-尝试打开.gitignore以在文本编辑器中对其进行编辑,但在OSXMountainLion上找不到文件位
我在游戏和帐户模型之间存在多对多关系,如下所示:classAccount:destroyhas_many:games,:through=>:account_gamesendclassGame:destroyhas_many:accounts,:through=>:account_gamesendclassAccountGame现在我知道让我们说我想创建一个类似这样的记录:@account=Account.new(params[:user])@account.games但是我应该如何在执行此操作时更新AccountGame中的某些属性?假设AccountGame有一些名为score的字段
我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::
我正在遍历数组中的一组标签名称,我想使用构建器打印每个标签名称,而不是求助于“我认为:builder=Nokogiri::XML::Builder.newdo|xml|fortagintagsxml.tag!tag,somevalendend会这样做,但它只是创建名称为“tag”的标签,并将标签变量作为元素的文本值。有人可以帮忙吗?这个看起来应该比较简单,我刚刚在搜索引擎上找不到答案。我可能没有以正确的方式提问。 最佳答案 尝试以下操作。如果我没记错的话,我添加了一个根节点,因为Nokogiri需要一个。builder=Nokogi
这是一些奇怪的例子:#!/usr/bin/rubyrequire'rubygems'require'open-uri'require'nokogiri'print"withoutread:",Nokogiri(open('http://weblog.rubyonrails.org/')).class,"\n"print"withread:",Nokogiri(open('http://weblog.rubyonrails.org/').read).class,"\n"运行此返回:withoutread:Nokogiri::XML::Documentwithread:Nokogiri::