我打电话的时候
addChild('actor', 'John Doe');
这个孩子在最后添加.有没有办法让这个新生儿成为第一个孩子?
说我有XML:
<root>
<nodeA />
<nodeA />
<nodeA />
<nodeC />
<nodeC />
<nodeC />
</root>
Run Code Online (Sandbox Code Playgroud)
如何在As和Cs之间插入"nodeB"?在PHP中,最好是通过SimpleXML?喜欢:
<root>
<nodeA />
<nodeA />
<nodeA />
<nodeB />
<nodeC />
<nodeC />
<nodeC />
</root>
Run Code Online (Sandbox Code Playgroud) 我正在创建一个Atom提要,当我在下面尝试添加xmlns:i属性时 -
$node->addAttribute("xmlns:i","http://www.w3.org/2001/XMLSchema-instance");
Run Code Online (Sandbox Code Playgroud)
我把它作为输出 -
i="http://www.w3.org/2001/XMLSchema-instance"
Run Code Online (Sandbox Code Playgroud)
"xmlns:"部分被切断了.我是否需要逃避:字符?或者他们是否有其他方式来添加此命名空间?
这是在print_r()典型的SimpleXMLElement对象上运行的输出:
SimpleXMLElement Object
(
[@attributes] => Array
(
)
)
Run Code Online (Sandbox Code Playgroud)
@符号是什么意思?
假设我有以下XML结构:
<?xml version="1.0" encoding="UTF-8"?>
<main>
<parent>
<child1>some value</child1>
<child2>another value</child2>
</parent>
</main>
Run Code Online (Sandbox Code Playgroud)
我创建了XML的变量,现在我想得到child1的值,所以我使用SimpleXML:
$xml = new SimpleXMLElement($xml);
$this->xmlcode = (string) $xml->main->parent->child1;
Run Code Online (Sandbox Code Playgroud)
但我收到此消息:注意:尝试在x行的/x.php中获取非对象的属性
我也用$ xml-> parent-> child1尝试过,但没有成功.
任何人??
我不确定为什么会这样.我没有序列化XML,而是我从RSS提要创建的数组(注意这只是一个片段):
$game_data = array (
'sysreqos' => $game->systemreq->pc->sysreqos,
'sysreqmhz' => $game->systemreq->pc->sysreqmhz,
'sysreqmem' => $game->systemreq->pc->sysreqmem,
'sysreqdx' => $game->systemreq->pc->sysreqdx,
'sysreqhd' => $game->systemreq->pc->sysreqhd,
);
Run Code Online (Sandbox Code Playgroud)
然后我序列化它$some_var = serialize($game_data)并写入文本文件fputs($fh,$some_var).
但它没有那么远,它在序列化行上出错:
未被捕获的异常'异常',消息'序列化'SimpleXMLElement'不允许'
我正在使用SimpleXML.如果我的函数的用户输入无效,我的变量$x是一个空的SimpleXMLElement对象; 否则,它有一个人口稠密的财产$x->Station.我想查看是否Station存在.
private function parse_weather_xml() {
$x = $this->weather_xml;
if(!isset($x->Station)) {
return FALSE;
}
...
}
Run Code Online (Sandbox Code Playgroud)
这样做我想要的,除了它返回一个错误:
警告:WeatherData :: parse_weather_xml():WeatherData-> parse_weather_xml()中不再存在节点(vvdtn.inc的第183行).
好的,好isset()了.我们试试这个:
private function parse_weather_xml() {
$x = $this->weather_xml;
if(!property_exists($x, 'Station')) {
return FALSE;
}
...
}
Run Code Online (Sandbox Code Playgroud)
这几乎完全相同:
警告:property_exists():WeatherData-> parse_weather_xml()中不再存在节点(vvdtn.inc的第183行)
好吧,很好,我会把它变成一个例外而忽视它.我以前从未这样做过,我不确定我做得对,但我会试一试:
function crazy_error($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
...
private function parse_weather_xml() {
set_error_handler('crazy_error');
$x = $this->weather_xml;
if(!property_exists($x, 'Station')) {
return FALSE;
}
restore_error_handler();
... …Run Code Online (Sandbox Code Playgroud) 是否可以XPath使用多个包含不同元素值的表达式?
XML
<data>
<person>
<firstname>Kerry</firstname>
<lastname>Packer</lastname>
<address>Crown</address>
<person>
<person>
<firstname>Kerry</firstname>
<lastname>Murdoch</lastname>
<address>California</address>
<person>
<data>
Run Code Online (Sandbox Code Playgroud)
PHP
$xml = simplexml_load_string($data);
$elements = $xml->xpath("(//person)[firstname[contains(., 'Kerr')]] and [lastname[contains(., 'och')]]");
Run Code Online (Sandbox Code Playgroud)
目前,上述XPath表达式被标记为无效.但是,如果我将它与一个元素一起使用,
$xml->xpath("(//person)[firstname[contains(., 'Kerr')]]");
Run Code Online (Sandbox Code Playgroud)
然后它工作正常.
我有一个XML文档,它附加了一个默认命名空间,例如
<foo xmlns="http://www.example.com/ns/1.0">
...
</foo>
Run Code Online (Sandbox Code Playgroud)
实际上,这是一个符合复杂模式的复杂XML文档.我的工作是从中解析出一些数据.为了帮助我,我有一个XPath的电子表格.XPath是相当深层嵌套的,例如
level1/level2/level3[@foo="bar"]/level4[@foo="bar"]/level5/level6[2]
Run Code Online (Sandbox Code Playgroud)
生成XPath的人是模式的专家,所以我假设我不能简化它,或者使用对象遍历快捷方式.
我正在使用SimpleXML来解析所有内容.我的问题与如何处理默认命名空间有关.
由于根元素上有一个默认命名空间,我不能这样做
$xml = simplexml_load_file($somepath);
$node = $xml->xpath('level1/level2/level3[@foo="bar"]/level4[@foo="bar"]/level5/level6[2]');
Run Code Online (Sandbox Code Playgroud)
我必须注册命名空间,将其分配给前缀,然后在我的XPath中使用前缀,例如
$xml = simplexml_load_file($somepath);
$xml->registerXPathNamespace('myns', 'http://www.example.com/ns/1.0');
$node = $xml->xpath('myns:level1/myns:level2/myns:level3[@foo="bar"]/myns:level4[@foo="bar"]/myns:level5/myns:level6[2]');
Run Code Online (Sandbox Code Playgroud)
从长远来看,添加前缀不会是可管理的.
是否有一种正确的方法来处理默认名称空间而无需使用XPath前缀?
使用空前缀不起作用($xml->registerXPathNamespace('', 'http://www.example.com/ns/1.0');).我可以指出默认的命名空间,例如
$xml = file_get_contents($somepath);
$xml = str_replace('xmlns="http://www.example.com/ns/1.0"', '', $xml);
$xml = simplexml_load_string($xml);
Run Code Online (Sandbox Code Playgroud)
但这是在绕过这个问题.
我有一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<xml>
<events date="01-10-2009" color="0x99CC00" selected="true">
<event>
<title>You can use HTML and CSS</title>
<description><![CDATA[This is the description ]]></description>
</event>
</events>
</xml>
Run Code Online (Sandbox Code Playgroud)
我使用xpath和xquery来解析xml.
$xml_str = file_get_contents('xmlfile');
$xml = simplexml_load_string($xml_str);
if(!empty($xml))
{
$nodes = $xml->xpath('//xml/events');
}
Run Code Online (Sandbox Code Playgroud)
我正在得到正确的标题,但我没有得到描述.如何我可以获得cdata内的数据