我对 PHP/XPath 还很陌生。我在 PHP 中使用 SimpleXML 和 XPath,通过名称空间遍历 XML。
我的问题是关于 registerXPathNamespace 函数中的“前缀”参数。
我的 xml feed 中有两个 xmlns;但是,我不确定在这种情况下如何使用该功能。
当我运行此代码时,我收到以下错误消息:
Undefined offset: 0
Run Code Online (Sandbox Code Playgroud)
我的 XML 无法以任何方式更改。请参阅下面的代码。感谢您提供任何线索。
PHP代码:
<?php
$url = 'test.xml';
$simplexml = simplexml_load_file($url);
//below, I'm uncertain as to what should be the prefix,
//in the case of no apparent prefix in the xml - I've seen 'a' in other
//examples (where the prefix does not exist in the xml), but unsure why.
$simplexml->registerXPathNamespace('a','http://www.digitalmeasures.com/schema/data');//
//below, as the 'dmd' prefix is in the …Run Code Online (Sandbox Code Playgroud) 如何在simplexml对象中获取最后一项(或任何特定项目)?假设您不知道将有多少个节点.
恩.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/xsl.xml"?>
<obj
href="http://xml.foo.com/"
display="com.foo.bar"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://obix.org/ns/schema/1.0"
>
<list name="data" of="HistoryRecord">
<obj>
<abstime name="timestamp" val="1876-11-10T00:00:00-08:00"></abstime>
<int name="energy_in_kwh" val="1234"></int>
<int name="energy_out_kwh" val="123456"></int>
</obj>
<obj>
<abstime name="timestamp" val="1876-11-10T00:15:00-08:00"></abstime>
<int name="energy_in_kwh" val="1335"></int>
<int name="energy_out_kwh" val="443321"></int>
</obj>
</list>
<int name="count" val="2"></int>
</obj>
Run Code Online (Sandbox Code Playgroud)
我想抓住最后<obj></obj>一块(或者甚至只是它的一部分).
我需要使用Simple XML加载XML源,与他的所有子项复制现有节点,然后在呈现XML之前自定义此新节点的属性.有什么建议吗?
$fp = fopen('data.txt', 'r');
$xml = new SimpleXMLElement('<allproperty></allproperty>');
while ($line = fgetcsv($fp)) {
if (count($line) < 4) continue; // skip lines that aren't full
$node = $xml->addChild('aproperty');
$node->addChild('postcode', $line[0]);
$node->addChild('price', $line[1]);
$node->addChild('imagefilename', $line[2]);
$node->addChild('visits', $line[3]);
}
echo $xml->saveXML();
Run Code Online (Sandbox Code Playgroud)
即时通讯使用此脚本将文本文件转换为xml文件,但我想将其输出到文件,我该怎么做这个simpleXML,干杯
我通过cUrl收到一个用PHP生成的XML代码:
$c = curl_init("http://www.domain.com/script.php");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$xmlstr = curl_exec($c);
Run Code Online (Sandbox Code Playgroud)
如果我回显$ xmlstr变量,它会显示以下内容:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<XGetPasoParadaREGResponse xmlns="http://tempuri.org/">
<XGetPasoParadaREGResult>
<PasoParada><cabecera>false</cabecera>
<e1>
<minutos>1</minutos>
<metros>272</metros>
<tipo>NORMAL</tipo>
</e1>
<e2>
<minutos>7</minutos>
<metros>1504</metros>
<tipo>NORMAL</tipo>
</e2><linea>28
</linea>
<parada>376</parada>
<ruta>PARQUE ALCOSA</ruta>
</PasoParada>
</XGetPasoParadaREGResult><status>1</status></XGetPasoParadaREGResponse>
</soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)
哪个是正确的,并且在脚本中生成的相同.但是,如果我尝试执行以下操作
$xml = simplexml_load_string($xmlstr);
if (!is_object($xml))
throw new Exception('Reading XML error',1001);
echo $xml->e1->minutos;
Run Code Online (Sandbox Code Playgroud)
不显示任何内容和print_r对象打印一个空对象.可能有什么不对?
我使用PHP和Simple XML.
我使用的循环不能像预期的那样工作:
foreach($item->Image->attributes()->source as $key => $value)
{
echo $value;
}
Run Code Online (Sandbox Code Playgroud)
在foreach中,我试图告诉我想要获取属性中列出的图像的"源".
$item上面是我上面的代码循环创建的foreach($xml_content->Section->Item as $item {}(如果你需要知道它来自哪里)
我的对象看起来像这样:
object(SimpleXMLElement)#36 (4) {
["Text"]=>
string(15) "Vinbergs socken"
["Description"]=>
string(73) "Vinbergs socken ingick i Faurås härad och ligger i Falkenbergs kommun.
"
["Url"]=>
string(44) "http://sv.wikipedia.org/wiki/Vinbergs_socken"
["Image"]=>
object(SimpleXMLElement)#38 (1) {
["@attributes"]=>
array(3) {
["source"]=>
string(113) "http://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Faur%C3%A5s_Vinberg.svg/50px-Faur%C3%A5s_Vinberg.svg.png"
["width"]=>
string(2) "50"
["height"]=>
string(2) "41"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的帖子开头有什么问题?
我正在使用simplexml_load_string解析xml,如下所示:
$xml = simplexml_load_string($response);
echo '{'.
'"Date":"'.$xml->Date[0].'",'.
'"Description":"'.$xml->ShipTos->ShipTo->ShippingGroups->ShippingGroup->OrderItems->OrderItem->Description[0].'",'.
'"Track":"'.$xml->Shipments->Shipment->Track[0].'"'.
'}';
Run Code Online (Sandbox Code Playgroud)
这可以正常工作,但如果一个节点多次出现在xml中,它只会抓取一次.有人可以帮我理解如何专门为Description节点编写foreach循环吗?
我有一个xml文件的URL,当我在浏览器中访问它时它看起来不错.但是,使用以下脚本,似乎缺少许多信息:
<?php
$xml_url = "example.xml";
$xml = simplexml_load_file($xml_url);
print_r($xml);
?>
Run Code Online (Sandbox Code Playgroud)
出了什么问题?
编辑:当我在浏览器中查看xml文件时,数据显示在这些之间:<![CDATA[ ]]>
也许这有帮助.
所以,我想最终为XML创建一个DOM对象,但是,$ xml_dom似乎是空的.var_dump显示对象(DOMElement)#3(0){}.$ xml_simple很好.我在这做错了什么?
$get_url_report = 'http://...'; // contains well-formatted XML data
$xml_simple = simplexml_load_string(file_get_contents($get_url_report));
$xml_dom = dom_import_simplexml($xml_simple);
Run Code Online (Sandbox Code Playgroud) 我有一个问题,答案肯定很简单,但是我缺乏理解。
我有一个具有以下外观的xml文件(简短示例)
<item id="1234">
<property name="country_id">
<value>4402</value>
</property>
<property name="rc_maintenance_other">
</property>
<property name="claim_right_shareholder">
</property>
<property name="charges_other">
</property>
<property name="other_expenses_heating">
</property>
<property name="unpaid_bills_amount">
</property>
<property name="iv_person_phone">
<value>03-6756711</value>
</property>
</item>
<item id="9876">
...
</item>
Run Code Online (Sandbox Code Playgroud)
我的问题是,我想从id为1234的一项中读取所有属性,并在数组中读取其属性和值(如果存在)。
我知道如何使用xpath访问特定项。(感谢这个精彩的stackoverflow社区:))
但是,如何仅对特定项目使用children()函数?
像这样
foreach ($item[id="1234"]->children() as $property) {
Run Code Online (Sandbox Code Playgroud)
非常感谢!