我有点困惑,我怎么能删除我可以通过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,它上面有很多命名空间,当我尝试用simpleXML加载它时,如果我指的是第一个命名空间我会得到一个xml对象,但是跟随其他命名空间的标记不会进入该对象.
我该如何解析这个XML?
<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header>
<eb:MessageHeader xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" eb:version="1.0" soap-env:mustUnderstand="1">
<eb:From>
<eb:PartyId eb:type="URI">wscompany.com</eb:PartyId>
</eb:From>
<eb:To>
<eb:PartyId eb:type="URI">mysite.com</eb:PartyId>
</eb:To>
<eb:CPAId>something</eb:CPAId>
<eb:ConversationId>moredata.com</eb:ConversationId>
<eb:Service eb:type="compXML">theservice</eb:Service>
<eb:Action>theaction</eb:Action>
<eb:MessageData>
<eb:MessageId>a certain messageid</eb:MessageId>
<eb:Timestamp>2009-04-11T18:43:58</eb:Timestamp>
<eb:RefToMessageId>mid:areference</eb:RefToMessageId>
</eb:MessageData>
</eb:MessageHeader>
<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext">
<wsse:BinarySecurityToken valueType="String" EncodingType="wsse:Base64Binary">an impresive binary security toekn</wsse:BinarySecurityToken>
</wsse:Security>
</soap-env:Header>
<soap-env:Body>
<SessionCreateRS xmlns="http://www.opentravel.org/OTA/2002/11" version="1" status="Approved">
<ConversationId>the goodbye token</ConversationId>
</SessionCreateRS>
</soap-env:Body>
</soap-env:Envelope>
Run Code Online (Sandbox Code Playgroud)
我试图用下面的代码解析它
<?php
$xml = simplexml_load_string($res,NULL,NULL,"http://schemas.xmlsoap.org/soap/envelope/");
?>
Run Code Online (Sandbox Code Playgroud)
但是$ xml对象只包含以下内容
SimpleXMLElement Object
(
[Header] => SimpleXMLElement Object
(
)
[Body] => SimpleXMLElement Object
(
)
)
Run Code Online (Sandbox Code Playgroud) 如何使用simpleXML编辑xml文件中的值?
我知道如何创建文件,但不知道如何编辑现有文件中的值?
如果你有类似的东西,
<hello id="1" name="myName1">
<anotherTag title="Hello">
</anotherTag>
</hello>
<hello id="2" name="myName2">
<anotherTag title="Hi">
</anotherTag>
</hello>
Run Code Online (Sandbox Code Playgroud)
如何将例如hello id 2的属性更改为name ="William"?或者标题嗨你好?
非常感谢你的关注,H'
我有两个XML树,并希望将一棵树作为叶子添加到另一棵树.
显然:
$tree2->addChild('leaf', $tree1);
Run Code Online (Sandbox Code Playgroud)
不起作用,因为它只复制第一个根节点.
好吧,那么我想我会遍历整个第一棵树,将每个元素逐个添加到第二棵树.
但是考虑这样的XML:
<root>
aaa
<bbb/>
ccc
</root>
Run Code Online (Sandbox Code Playgroud)
我如何访问"ccc"?tree1->children()只返回"bbb"....
我有这个simplexml结果对象:
object(SimpleXMLElement)#207 (2) {
["@attributes"]=>
array(1) {
["version"]=>
string(1) "1"
}
["weather"]=>
object(SimpleXMLElement)#206 (2) {
["@attributes"]=>
array(1) {
["section"]=>
string(1) "0"
}
["problem_cause"]=>
object(SimpleXMLElement)#94 (1) {
["@attributes"]=>
array(1) {
["data"]=>
string(0) ""
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要检查节点"problem_cause"是否存在.即使它是空的,结果也是错误的.在php手册上,我找到了我为我的需求修改的PHP代码:
function xml_child_exists($xml, $childpath)
{
$result = $xml->xpath($childpath);
if (count($result)) {
return true;
} else {
return false;
}
}
if(xml_child_exists($xml, 'THE_PATH')) //error
{
return false;
}
return $xml;
Run Code Online (Sandbox Code Playgroud)
我不知道用什么代替xpath查询'THE_PATH'来检查节点是否存在.或者将simplexml对象转换为dom更好吗?
我正在尝试使用PHP文档使用PHP 5的内置SimpleXMLElement类构建XML文档(用于AJAX).我想从一个空白的平板开始并按元素构建XML元素,但是我发现如果SimpleXMLElement不从一些现有的XML代码开始构建一个,我就没有办法.我无法成功地将空字符串("")传递给SimpleXMLElement构造函数,所以目前我正在传递最外层标记的XML,然后从那里构建.这是我的代码:
// Start with a "blank" XML document.
$xml = new SimpleXMLElement("<feature></feature>");
// Add various children and attributes to the main tag.
$xml->addAttribute("id", $id);
$xml->addChild("title", $feature['title']);
$xml->addChild("summary", $feature['summary']);
// ...
// After the document has been constructed, echo out the XML.
echo $xml->asXML();
Run Code Online (Sandbox Code Playgroud)
有更清洁的方法吗?
$xml是一个SimpleXML对象.
print_r($xml->Title);
Run Code Online (Sandbox Code Playgroud)
输出 SimpleXMLElement Object ( [0] => K&H 3093 Extreme Weather Kitty Pad with Fleece Cover )
如何访问第一个元素?
print_r($xml->Title[0]);
Run Code Online (Sandbox Code Playgroud)
没有输出!
我从谷歌结帐响应中解析XML时遇到了一些麻烦.XML直接来自谷歌服务器,所以XML本身没有问题.
我想掌握所有新订单通知标签
我试过这个,但每次都返回一个空数组().
$xml = new SimpleXmlElement($raw_xml);
$notifications = $xml->xpath('notifications');
$notifications = $xml->xpath('/notification-history-response/notifications/new-order-notification');
$notifications = $xml->xpath('//new-order-notification');
Run Code Online (Sandbox Code Playgroud)
一个XML snipet(刚刚开始)
<notification-history-response xmlns="http://checkout.google.com/schema/2" serial-number="c5cda190-0eb1-4f91-87cd-e656e5598d38">
<notifications>
<new-order-notification serial-number="271578974677716-00001-7">
<buyer-billing-address>
<address1>19 sandbox st</address1>
<address2></address2>
Run Code Online (Sandbox Code Playgroud) 我正在使用cURL向SOAP服务发送请求,我在POST Body中发送包含参数的XML,作为回应我收到:
Web服务:http://lcbtestxmlv2.ivector.co.uk/soap/book.asmx?WSDL
<?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>
<SearchResponse xmlns="http://ivectorbookingxml/">
<SearchResult>
<ReturnStatus>
<Success>true</Success>
<Exception />
</ReturnStatus>
<SearchURL>http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5&regionid=9</SearchURL>
<PropertyResults>
<PropertyResult>
<TotalProperties>215</TotalProperties>
<PropertyID>1795</PropertyID>
<PropertyName>Hotel Gaddis</PropertyName>
<Rating>3.0</Rating>
<Country>Egypte</Country>
<Resort>Louxor</Resort>
<Strapline>Cet établissement confortable propose un très bon service à un bon rapport qualité-prix. Cet hôtel de 6 étages compte 55 chambres et comprend une terrasse, une réception avec coffre-fort et ascenseur,</Strapline>
<Description>Cet établissement confortable propose un très bon service à un bon rapport qualité-prix. Cet hôtel de 6 …Run Code Online (Sandbox Code Playgroud) php ×10
simplexml ×10
xml ×3
soap ×2
attributes ×1
exists ×1
namespaces ×1
set ×1
web-services ×1
xpath ×1