I have a xml document, i need to get name attribute's value with helping php. the xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:GetGoodsTreeResponse xmlns:ns2="http://b2b.alta.com.ge" xmlns:ns3="http://192.168.0.10/b2b">
<ns3:GoodsTree level="0">
<item id="010000000017337" level="0" name="COMPUTERS" is_open="N">
<item id="015000000030431" level="1" name="ALTA" is_open="Y">
<item id="015000000030443" level="2" name="Zakaznoe Izdelie" is_open="N"/>
<item id="015002000031034" level="2" name="ATOM" is_open="N"/>
<item id="015005000030453" level="2" name="Celeron" is_open="N"/>
<item id="015010000030432" level="2" name="Dual Core" is_open="N"/>
<item id="015150000030778" level="2" name="i3" is_open="N"/>
<item id="015220000030775" level="2" name="i5" is_open="N"/>
<item id="015300000031827" level="2" name="i7" is_open="N"/>
</item>
<item id="010001005030300" level="1" name="Apple" is_open="N"/>
<item id="010001001033496" level="1" name="Asus" is_open="N"/>
<item id="010001001015793" level="1" name="Fujitsu" is_open="N"/>
<item id="010001002015166" level="1" name="HP Compaq" is_open="N"/>
</item>
</ns3:GoodsTree>
</ns2:GetGoodsTreeResponse>
</S:Body>
</S:Envelope>
please help me i dont know what to do.. sorry for my english.
You can use DOMDocument to parse that XML and get all items using DOMXpath, then loop in all items and get the attributes based on position (id = 0, name = 2), then create an new array that will hold all you item id's with their names:
$dom = new DOMDocument;
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('S', 'http://schemas.xmlsoap.org/soap/envelope/');
$items = array();
$el = $xpath->query('//item');
foreach($el as $item){
$attributes = $item->attributes;
$items[$attributes->item(0)->value] = $attributes->item(2)->value;
}
var_dump($items); // $items will be an array with item id and it's value will be item name
Codepad Example
Related
The soap xml reponse is as such:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:NS1="urn:TPAPIPosIntfU-ITPAPIPOS">
<NS1:GetArticlesInfoResponse xmlns:NS2="urn:TPAPIPosIntfU" xmlns:NS3="urn:TPAPIPosTypesU">
<return xsi:type="NS2:TGetArticlesInfoResponse">
<ReturnCode xsi:type="xsd:int">0</ReturnCode>
<ReturnMessage xsi:type="xsd:string">ok</ReturnMessage>
<Articles xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TArticleInfo[x]">...</Articles>
<Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
</return>
</NS1:GetArticlesInfoResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And this is the Articles array that i need to parse:
<item xsi:type="NS3:TArticleInfo">
<ArticleId xsi:type="xsd:long">5000001716</ArticleId>
<ArticleName xsi:type="xsd:string">Coca Cola</ArticleName>
<ArticleNumber xsi:type="xsd:int">1</ArticleNumber>
<Available xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:long[3]">
<item>5000000210</item>
<item>5000000208</item>
<item>5000000209</item>
</Available>
<DepartmentId xsi:type="xsd:long">5000000170</DepartmentId>
<Prices xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TItemPrice[2]">
<item xsi:type="NS3:TItemPrice">
<ArticleId xsi:type="xsd:long">5000001716</ArticleId>
<PriceId xsi:type="xsd:long">5000000206</PriceId>
<Amount xsi:type="xsd:double">2</Amount>
<Vat xsi:type="xsd:double">21</Vat>
<Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
</item>
<item xsi:type="NS3:TItemPrice">
<ArticleId xsi:type="xsd:long">5000001716</ArticleId>
<PriceId xsi:type="xsd:long">5000000207</PriceId>
<Amount xsi:type="xsd:double">1.7</Amount>
<Vat xsi:type="xsd:double">12</Vat>
<Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
</item>
</Prices>
<FreeOption xsi:type="xsd:long">5000000145</FreeOption>
<Options xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:long[1]">
<item>5000000143</item>
</Options>
<IsMenu xsi:type="xsd:boolean">false</IsMenu>
<IsManualPrice xsi:type="xsd:boolean">false</IsManualPrice>
<IsActive xsi:type="xsd:boolean">true</IsActive>
<Promo xsi:type="xsd:boolean">false</Promo>
<HqId xsi:type="xsd:string">Coca Cola</HqId>
<Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[5]">
<item xsi:type="NS3:TExtraInfo">
<Key xsi:type="xsd:string">daily_stock_active</Key>
<Value xsi:type="xsd:string">0</Value>
<Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
</item>
<item xsi:type="NS3:TExtraInfo">
<Key xsi:type="xsd:string">daily_stock_qty</Key>
<Value xsi:type="xsd:string">0</Value>
<Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
</item>
<item xsi:type="NS3:TExtraInfo">
<Key xsi:type="xsd:string">purchase_price</Key>
<Value xsi:type="xsd:string">0.0000</Value>
<Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
</item>
<item xsi:type="NS3:TExtraInfo">
<Key xsi:type="xsd:string">course_id</Key>
<Value xsi:type="xsd:string">5000001331</Value>
<Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
</item>
<item xsi:type="NS3:TExtraInfo">
<Key xsi:type="xsd:string">info</Key>
<Value xsi:type="xsd:string"/>
<Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
</item>
</Extra>
</item>
Parameter Type Description
ArticleId Long Internal Id of this Article
ArticleName String Name of this Article
ArticleNumber Integer Number of this Article
Available [Long] List of Sales Area Id's where this Article is available
Prices [ItemPrice] List of Prices for this Article
DepartmentId Long Id of the Department where this Article belongs to
FreeOption Long Free Option ID
Options [Long] List of must-have options
IsMenu Boolean Returns true if this Article is a menu
IsManualPrice Boolean Returns true if this Article requires a manual price input when ordered
IsActive Boolean Returns if true is this Article is active
Promo Boolean Return true if this Article is a Cobmo/Promo article
HqId String HQ Id
Extra [ExtraInfo] List of extra fields
List of available extra fields:
Key Description
course_id Only return articles of a specific course
daily_stock_active 1/0 (if DailyStock=1 was specified in request’s Extra fields)
daily_stock_qty Daily stock quantity (if DailyStock=1 was specified in request’s Extra fields)
info Article info (tab 9 of the article settings in back-office)
plu When PLU is not zero
purchase_price Purchase price
ArticleShort
ArticleShort Object
<item xsi:type="NS3:TArticleShort">
<ArticleId xsi:type="xsd:long">5000001716</ArticleId>
<ArticleName xsi:type="xsd:string">Coca Cola</ArticleName>
<ArticleNumber xsi:type="xsd:int">1</ArticleNumber>
<SalesAreaId xsi:type="xsd:long">5000000210</SalesAreaId>
<DepartmentId xsi:type="xsd:long">5000000170</DepartmentId>
<HqId xsi:type="xsd:string">Coca Cola</HqId>
<Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
</item>
How can I parse this soap XML response in php curl to get the Articles array data? I have tried SimpleXMLElement but I am unable to parse this.
I have surfed a lot for answers but not able to get the correct parsing technique. Shall I use Xpaths if yes, then how to create an XPath to parse the articles array?
A simple solution using XPath and SimpleXML, which from the look of the XML doesn't involve namespaces for the XPath itself. This loads the XML and then looks for an element called <return> with a direct descendant called <Articles>. As xpath() returns a list of nodes, it then just picks the first one (using [0]) and iterates of the <item> elements enclosed and outputs the <ArticleId> elements value....
$xml = simplexml_load_string($data);
$articles = $xml->xpath("//return/Articles");
foreach ( $articles[0]->item as $item ) {
echo (string)$item->ArticleId.PHP_EOL;
}
With the test data this gives...
5000001716
5000001716
HI I have a php script which finds certain Words in an XML file. I would like to add a new XML element if a certain word was found at the end of the file. But in my code it add one every time it finds one.
What I am doing wrong?
XML:
<products>
<product>
<title>TestProduct</title>
<Specifications>
<item name="Specifications1">Test</item>
<item name="Specifications2">Hello World</item>
</Specifications>
<body>
<item name="Color">Black</item>
</body>
</product>
</products>
PHP:
$dom = new DOMDocument;
$dom->load('Test.xml');
$xpath = new DOMXPath($dom);
foreach ($xpath->query("//*[contains(., 'Black')]") as $item) {
$element = $dom->createElement('ID', '123');
$item->appendChild($element);
}
echo $dom->saveXML();
should look like that:
<products>
<product>
<title>TestProduct</title>
<Specifications>
<item name="Specifications1">Test</item>
<item name="Specifications2">Hello World</item>
</Specifications>
<body>
<item name="Color">Black</item>
</body>
<ID>123</ID>
</product>
</products>
If I undertood correctly, you can your change xpath with
//product[contains(body/item, 'Black')]
Then the code will add new ID tag to the product, having item with the value 'Black'
demo
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i have a problem with reading my xml. By code down i read xml, bud how i say at the end of this post, i need read subelements. But i don't know how.
$reader = new XMLReader();
$reader->open("data/sns.xml");
while($reader->read()){
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "item" && $reader->getAttribute('sensor') == "th0" && $reader->getAttribute('cat') == "temp" && $reader->getAttribute('unit') == "c") {
echo "Venkovní teplota: ".$reader->readString()." °C";
echo "<br />";
}
if($reader->nodeType == XMLReader::ELEMENT && $reader->name == "item" && $reader->getAttribute("sensor") == "th0" && $reader->getAttribute("cat") == "hum" && $reader->getAttribute("unit") == "rel"){
echo "venkovní vlhkost: ".$reader->readString();
echo "<br />";
}}
I can read this, but my xml looks like:
<data timeframe="actual">
<item sensor="system" cat="version" unit="text">4.9u</item>
<item sensor="system" cat="version" unit="num">49</item>
<item sensor="system" cat="build" unit="num">1261</item>
<item sensor="system" cat="platform" unit="text">SilverStone_DC01</item>
<item sensor="system" cat="language" unit="text">Czech</item>
<item sensor="system" cat="temp" unit="unit">c</item>
<item sensor="system" cat="hum" unit="unit">rel</item>
<item sensor="system" cat="press" unit="unit">hpa</item>
<item sensor="system" cat="wind" unit="unit">ms</item>
<item sensor="system" cat="rain" unit="unit">mm</item>
<item sensor="date0" cat="date" unit="utc">20160111090606</item>
<item sensor="date0" cat="date2" unit="utc">11.01.2016 09:06:06</item>
<item sensor="date0" cat="puredate" unit="utc">11.01.2016</item>
<item sensor="date0" cat="time" unit="utc">09:06:06</item>
<item sensor="date0" cat="year" unit="utc">2016</item>
<item sensor="date0" cat="month" unit="utc">01</item>
<item sensor="date0" cat="day" unit="utc">11</item>
<item sensor="date0" cat="dayofweek" unit="utc">1</item>
<item sensor="date0" cat="hour" unit="utc">09</item>
<item sensor="date0" cat="min" unit="utc">06</item>
<item sensor="date0" cat="sec" unit="utc">06</item>
<item sensor="date0" cat="date" unit="local">20160111100606</item>
<item sensor="date0" cat="date2" unit="local">11.01.2016 10:06:06</item>
<item sensor="date0" cat="puredate" unit="local">11.01.2016</item>
<item sensor="date0" cat="time" unit="local">10:06:06</item>
<item sensor="date0" cat="year" unit="local">2016</item>
<item sensor="date0" cat="month" unit="local">01</item>
<item sensor="date0" cat="day" unit="local">11</item>
</data>
and i need read for example. sensor: date0 and cat: date2 and unit: utc and now show me a string.
When you are looking for this code u say it is easy, but in my xml i have next tag
<data timeframe="lastday"></data>
and the same items, bud i need to read only actual.
Please help.
Thank You
Make yourself familiar with SimpleXML and xpath, with these your task is very simple:
<?php
$string = __your_xml_string_here;
$xml = simplexml_load_string($string);
$sensors = $xml->xpath("//item[#sensor='date0']");
print_r($sensors);
# or loop over them with foreach
foreach ($sensors as $sensor) {
// do sth. useful here
// the full string can be obtained with
// $sensor->asXML() or $sensor->__toString()
}
?>
$xml = new DOMDocument();
$root=$xml->createElement("ROOT");
$xml->appendChild($root);
$data=$xml->createElement("DATA");
while($row=db_fetch_object($result))
{
$data=$xml->createElement("ITEM");
$item->setAttribute("COMPANY",$row->field_windmill_fabrikant_value);
$item->setAttribute("HEIGHT",$row->field_windmill_ashoogte_value);
$item->setAttribute("POWER",$row->field_windmill_vermogen_value);
$item->setAttribute("LOCATION",$row->field_windmill_provincie_value);
$item->setAttribute("START_YEAR",$row->field_windmill_startjaar_value);
$data->appendChild($item);
}
$root->appendChild($data);
echo $xml->saveXML();
Here I want to append ITEM as a child node to data but ITEM is getting appended to item and not to data. I'm using PHP.
Can anyone help in it.
Thanks.
Just replace
$data=$xml->createElement("ITEM");
with
$item=$xml->createElement("ITEM");
result of this will be
<?xml version="1.0"?>
<ROOT>
<DATA>
<ITEM COMPANY="COMPANY0" HEIGHT="HEIGHT0" POWER="POWER0" LOCATION="LOCATION0" START_YEAR="START_YEAR0"/>
<ITEM COMPANY="COMPANY1" HEIGHT="HEIGHT1" POWER="POWER1" LOCATION="LOCATION1" START_YEAR="START_YEAR1"/>
<ITEM COMPANY="COMPANY2" HEIGHT="HEIGHT2" POWER="POWER2" LOCATION="LOCATION2" START_YEAR="START_YEAR2"/>
</DATA>
</ROOT>
my xml structure is:
<users>
<user id="126">
<name>老黄牛三</name>
<watchHistory>
<whMonthRecords month="2010-10">
<whDateList month="2010-10">
<date>01</date>
<date>02</date>
<date>05</date>
<date>08</date>
<date>21</date>
</whDateList>
<whDateRecords date="2010-10-01">
<item itemID="1">飞越疯人院.老黄牛三.2010-10-01</item>
<item itemID="4">回到未.老黄牛三.2010-10-01来</item>
<item itemID="5">天天看的哦啊你.2010-10-01来</item>
</whDateRecords>
<whDateRecords date="2010-10-05">
<item itemID="1">飞越疯人院.老黄牛三.2010-10-05</item>
<item itemID="4">回到未来.老黄牛三.2010-10-05</item>
</whDateRecords>
</whMonthRecords>
<whMonthRecords month="2010-11">
........
</whMonthRecords>
<watchHistory>
</user>
</users>
now, how can I add child :
<whDateRecords date="2010-10-06">
<item itemID="45">飞越疯人院.老黄牛三.2010-10-05</item>
<item itemID="432">回到未来.老黄牛三.2010-10-05</item>
</whDateRecords>
to the node:<whMonthRecords month="2010-10">
Thank you very much!
First, look for the parent of the node you want to add, say you want to add it to the node with month 2010-10, use this xpath:
$xpath = '//whMonthRecords[#month="2010-10"]';
$nodes = $sxml->xpath($xpath); //sxml is the xml object!
$parent = $nodes[0];
Now that you have the parent, you can add the node using addChild method.