我想设置xpath()找到的某个节点的文本
<?php
$args = new SimpleXmlElement(
<<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML
);
// I want to set text of some node found by xpath
// Let's take (//c) for example
// convoluted and I can't be sure I'm setting right node
$firstC = reset($args->xpath("//c[1]/parent::*"));
$firstC->c[0] = "test 1";
// like here: Found node is not actually third in its parent.
$firstC = reset($args->xpath("(//c)[3]/parent::*"));
$firstC->c[2] = "test 2";
// following won't work for obvious reasons, …Run Code Online (Sandbox Code Playgroud) 我有点困惑,我怎么能删除我可以通过xpath搜索找到的东西的父节点:
$xml = simplexml_load_file($filename);
$data = $xml->xpath('//items/info[item_id="' . $item_id . '"]');
$parent = $data[0]->xpath("parent::*");
unset($parent);
Run Code Online (Sandbox Code Playgroud)
所以,它找到了项目ID,没有问题 - 但是没有设置不能摆脱这个<items>节点.我想要做的就是删除<items>...</items>此产品.显然,<items>xml文件中存在大量节点,因此无法unset($xml->data->items)删除所有节点.
任何想法非常赞赏:-)
我有一个这种结构的xml文件
<?xml version="1.0" encoding="iso-8859-1"?>
<my_events>
<event id="e20111129215359">
<title>the title</title>
<channel id="1">
<name>A name</name>
<onclick></onclick>
</channel>
<event_site>
<name/>
<url/>
</event_site>
<start_date>Thu Mar 08 2012</start_date>
<start_time>11:00 AM</start_time>
<end_date>null</end_date>
<end_time>null</end_time>
<notes>Notes for the event</notes>
</event>
</my_events>
Run Code Online (Sandbox Code Playgroud)
要删除一个事件,我有这个PHP功能.
<?php
include_once("phpshared.php");
function delete_event( $nodeid ) {
$nodes = new SimpleXMLElement('my_events.xml', LIBXML_NOCDATA, true);
$node = $nodes->xpath("/my_events/event[@id='$nodeid']");
$node->parentNode->removeChild($node);
$formatted = formatXmlString($nodes->asXML());
$file = fopen ('my_events.xml', "w");
fwrite($file, $formatted);
fclose ($file);
}
echo delete_event(trim($_REQUEST['nodeid']));
?>
Run Code Online (Sandbox Code Playgroud)
这不会删除节点.有没有不同的方法来做到这一点?
如此真气,我几乎无法说话.我已经使用SimpleXML组装了一个RSS提要 - 但它正在使用名称空间,现在就是这样.但是,当输出时,它会不断尝试在根节点中声明xmlns:xmlns ="".即使我没有这样的事情.
它始于
$rssXML->addAttribute("version", '2.0');
$rssXML->addAttribute("xmlns:media", "http://search.yahoo.com/mrss/", '');
$rssXML->addAttribute("xmlns:dcterms", "http://purl.org/dc/terms/", '');
Run Code Online (Sandbox Code Playgroud)
在此之后我做: -
header("Content-Type: application/rss+xml");
echo $syndicationXML->asXML();
Run Code Online (Sandbox Code Playgroud)
然而它输出: -
<?xml version="1.0"?>
<rss xmlns:xmlns="" version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:dcterms="http://purl.org/dc/terms/"><channel>...
Run Code Online (Sandbox Code Playgroud)
我不明白所有这个名称空间声明.这是怎么回事?
我想在再次填充之前从我的 xml 文件中删除所有子项(或创建更新,但这似乎更难)。所以我所做的是
$file = "data.xml";
$xml=simplexml_load_file($file);
$teller=0;
foreach( $entries as $entry ) {
foreach ($xml->xpath('//concerts') as $desc) {
if($teller == 0)
{
$lol=$desc->children();
unset($lol);
}
$concert = $desc->addChild( 'concert' );
$concert->addChild( 'artist', array_shift( $entry ) );
$concert->addChild( 'location', array_shift( $entry ) );
$concert->addChild( 'date', array_shift( $entry ) );
$teller++;
}
}
file_put_contents($file, $xml->asXML());
Run Code Online (Sandbox Code Playgroud)
但这并不能消除任何东西,对我做错了什么有什么想法吗?
我有一个只运行一次的foreach循环,它让我难过.
1:我加载了一系列状态值("请求","删除"或"已购买")
2:然后我加载一个xml文件,需要循环"代码"节点并更新它们的状态,但是如果新代码是"删除"我想删除它然后转移到下一个
XML结构是......
<content>
.... lots of stuff
<codes>
<code date="xxx" status="request">xxxxx</code>
.. repeat ...
</codes>
</content>
Run Code Online (Sandbox Code Playgroud)
和PHP代码是......
$newstatus = $_POST['updates'];
$file = '../apps/templates/'.$folder.'/layout.xml';
$xml2 = simplexml_load_file($file);
foreach($xml2->codes->code as $code){
if($code['status'] == "delete") {
$dom=dom_import_simplexml($code);
$dom->parentNode->removeChild($dom);
}
}
$xml2->asXml($file);
Run Code Online (Sandbox Code Playgroud)
我暂时删除了更新,因此我可以调试删除检查.这一切都有效,但它只删除第一个删除并留下所有其他删除,即使它是一个foreach循环?任何帮助非常感谢.