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()
}
?>
Related
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
I am trying to make use of the stand soapClient for PHP. I can make a class successfully using SOAP UI however when I trry and make that call using PHP this is the difference.
<arrInputs xsi:type="ns1:KeyValueArrayInput" SOAP-ENC:arrayType="ns1:KeyValueArray[]" >
<item xsi:type="ns1:KeyValueArray">
<Key xsi:type="xsd:string">Realm</Key>
<Value xsi:type="xsd:string">test</Value>
</item>
<item xsi:type="ns1:KeyValueArray">
<Key xsi:type="xsd:string">UserName</Key>
<Value xsi:type="xsd:string">test15</Value>
</item>
Now when I try send it via the php soap client I get this.
<arrInputs SOAP-ENC:arrayType="ns1:KeyValueArray[22]" xsi:type="ns1:KeyValueArrayInput">
<item xsi:type="ns1:KeyValueArray">
<Key xsi:type="xsd:string">Realm</Key>
<Value xsi:type="xsd:string">test</Value>
</item>
<item xsi:type="ns1:KeyValueArray">
<Key xsi:type="xsd:string">UserName</Key>
<Value xsi:type="xsd:string">test2</Value>
</item>
Any suggestions as to how the type and the array type have been switched around. If I put the type in front of the arrayType all is good.
$creationFields = array();
$creationFields['strSessionID'] = $this->getSessionHash();
$arrInputs = array();
foreach ($params as $k => $v)
{
$arrInputs[] = array('Key' => $k, 'Value' => $v);
}
$creationFields['arrInputs'] = $arrInputs;
__soapCall('methodName', $creationFields);
This is out of the WSDL:
<message name="methodName"><part name="strSessionID" type="xsd:string" /><part name="arrInputs" type="tns:KeyValueArrayInput" /></message>
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A simple program to CRUD node and node values of xml file
I would like to change some data in a xml file with php.
So in my xml file I have this :
<items><item href="p002.xhtml" id="p002" media-type="application/xhtml+xml"/>
<item href="img/b002.jpg" id="b002" media-type="image/jpeg"/>
<item href="p003.xhtml" id="p003" media-type="application/xhtml+xml"/>
<item href="img/b003.jpg" id="b003" media-type="image/jpeg"/>
<item href="p004.xhtml" id="p004" media-type="application/xhtml+xml"/>
<item href="img/b004.jpg" id="b004" media-type="image/jpeg"/>
<item href="p005.xhtml" id="p005" media-type="application/xhtml+xml"/>
<item href="img/b005.jpg" id="b005" media-type="image/jpeg"/></items>
I would like to open the file, delete all item greater than 0003 and save changes. I have some tags between this extract.
Result:
<items><item href="p002.xhtml" id="p002" media-type="application/xhtml+xml"/>
<item href="img/b002.jpg" id="b002" media-type="image/jpeg"/>
<item href="p003.xhtml" id="p003" media-type="application/xhtml+xml"/>
<item href="img/b003.jpg" id="b003" media-type="image/jpeg"/></items>
Trying :
if( ! $xml = simplexml_load_file("pack.xml") ) {
echo 'unable to load XML file';
}
else {
echo "<ul>";
foreach($xml->items->item as $v ){
//echo "<li>".$v[href]."</li>";
if($v[href]=="p003.xhtml" || $v[href]=="img/bg003.jpg"){
echo "<li>".$v[href]."</li>";
}
}
echo "</ul>";
}
How to check the greater than p003.xhtml and img/bg003.jpg ?
You are going to want to parse the file using simplexml. This will generate a nice set of DOM like classes for you to work with. From these classes you can search for elements with your criteria and remove them before saving the xml tree back to text
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.