php simplexml not read <sheet r:id="rId1"/> - php

this is my code
$str = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheet r:id="rId1"/></workbook>';
var_dump(simplexml_load_string($str));exit;
output:
object(SimpleXMLElement)#1 (1) { ["sheet"]=> object(SimpleXMLElement)#2 (0) { } }
rId1 doesn't show,why?

I would use Xpath to get that attribute.
<?php
$str = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheet r:id="rId1"/></workbook>';
$xml = simplexml_load_string($str);
$id = $xml->xpath('sheet/#r:id');
echo 'r:id= ' . $id[0]
?>
Output:
r:id = rId1

Related

XML Parsing in PHP (using simplexml_load_string)

I have the following code and I have been working to try to get this working.
<?php declare(strict_types=1);
$session_token = '?'; $xml = '';
$result = '<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ws.careerbuilder.com/resumes/"><Packet><Errors /><SessionToken>3msk323msd-3312-CQ-2</SessionToken></Packet></string>
';
if ($result) {
$xml = simplexml_load_string($result);
print_r($xml);
if ($xml !== false) {
$session_token = $xml->SessionToken;
echo PHP_EOL.'Session: '. $session_token;
} else {
echo 'Error: XML does NOT appear to be valid';
}
} else
echo 'Error: result does NOT appear be valid';
The problem is no matter what I'm not able to extract the <SessionToken> value from the XML. When I use print_r() I get the following:
SimpleXMLElement Object
(
[0] => <Packet><Errors /><SessionToken>3msk323msd-3312-CQ-2</SessionToken></Packet>
)
Your input is entity-encoded. If this is really what it looks like, you'll need to decode it first:
$xml = simplexml_load_string(html_entity_decode($result));
$token = (string) $xml->Packet->SessionToken[0];
You document contains nested XML. The text content of the string element is serialized XML. So you need to parse it after reading it.
$result = '<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ws.careerbuilder.com/resumes/"><Packet><Errors /><SessionToken>3msk323msd-3312-CQ-2</SessionToken></Packet></string>
';
$string = new SimpleXMLElement($result);
$packet = new SimpleXMLElement((string)$string);
var_dump($packet);
Output:
object(SimpleXMLElement)#2 (2) {
["Errors"]=>
object(SimpleXMLElement)#3 (0) {
}
["SessionToken"]=>
string(20) "3msk323msd-3312-CQ-2"
}

Getting only the child elements of the first element

My XML file is such as (for example):
<?xml version="1.0" encoding="UTF-8" ?>
<bikes>
<bike>
<model>First</model>
<speedNumber>4</speedNumber>
<sizes>100</sizes>
<amount>100</amount>
</bike>
<bike>
<model>Second</model>
<speedNumber>3</speedNumber>
<sizes>300</sizes>
<amount>150</amount>
</bike>
<bike>
<model>Third</model>
<speedNumber>4</speedNumber>
<sizes>300</sizes>
<amount>300</amount>
</bike>
</bikes>
How can I get child elements for the first <bike> element only?
I want to print model=First, speedNumber=4 ...
<bike>
<model>First</model>
<speedNumber>4</speedNumber>
<sizes>100</sizes>
<amount>100</amount>
</bike>
I tried this:
foreach ($xml->children(0) as $bikes)
{
foreach ($bikes->children() as $childs)
{
echo $childs->getName()."=".$childs ." ";
}
echo "<br>";
}
But it doesn't work for me.
How can I solve this problem?
You can access the elements directly using array syntax:
$bikes->bike[0]->model
$bikes->bike[0]->speedNumber
...
Full code:
<?php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<bikes>
<bike>
<model>First</model>
<speedNumber>4</speedNumber>
<sizes>100</sizes>
<amount>100</amount>
</bike>
<bike>
<model>Second</model>
<speedNumber>3</speedNumber>
<sizes>300</sizes>
<amount>150</amount>
</bike>
<bike>
<model>Third</model>
<speedNumber>4</speedNumber>
<sizes>300</sizes>
<amount>300</amount>
</bike>
</bikes>
XML;
$bikes = new SimpleXMLElement($xml);
echo $bikes->bike[0]->model . "\n";
echo $bikes->bike[0]->speedNumber . "\n";
echo $bikes->bike[0]->sizes . "\n";
echo $bikes->bike[0]->amount . "\n";
?>
Tested here: http://codepad.org/yVAyoPtG

Get data from an xml file

In a php script how to read and convert a XML document to an object and access the obtained object in order to get his data?
<?php
$xml ='<?xml version="1.0" encoding="UTF-8" ?>
<data request-id="ID">
<data name="Name1"
d1="0"
d2="0231234"
d3="32584">
<data name="Name2"
d4="231234"
d5="2012-06-06 18:18:10.000607"
d6="3b048653-aaa9-485b-b0dd-d16e068230e9" />
</data>
</data>';
$xml = simplexml_load_string($xml);
//how to get the data d1? or d4? from the obtained object
?>
You can use this snippet:
<?php
$xmlstring = file_get_contents($filename);
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$object = json_decode($json);
Try using this function -
$xml ='<?xml version="1.0" encoding="UTF-8" ?>
<data request-id="ID">
<data name="Name1"
d1="0"
d2="0231234"
d3="32584">
<data name="Name2"
d4="231234"
d5="2012-06-06 18:18:10.000607"
d6="3b048653-aaa9-485b-b0dd-d16e068230e9" />
</data>
</data>';
function xmlToArray($input, $callback = null, $recurse = false) {
$data = ((!$recurse) && is_string($input))? simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA): $input;
if ($data instanceof SimpleXMLElement) $data = (array) $data;
if (is_array($data)) foreach ($data as &$item) $item = xmlToArray($item, $callback, true);
return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data): $data;
}
$xml = xmlToArray($xml);
echo $xml['data']['#attributes']['d1'];
echo '<br/>';
echo $xml['data']['data']['#attributes']['d4'];
Use
$myxml = simplexml_load_string($xml);
echo $myxml->data[1]['d1'];
echo $myxml->data[1]['d2'];
echo $myxml->data[1]['d3'];
echo $myxml->data[2]['d4'];
Reference : How to parse XML with PHP5
try :-
echo $xml['data']['#attributes']['d1'];
echo '<br/>';
echo $xml['data']['data']['#attributes']['d4'];

Can I use SQL syntax on SimpleXML?

Can I use a WHERE selector (like SQL syntax) on SimpleXML?
Example XML
<?xml version="1.0" encoding="utf-8" ?>
<documentElement>
<row>
<id>1</id>
<name>David</name>
<surname>Johnson</surname>
</row>
<row>
<id>2</id>
<name>Jack</name>
<surname>Nixon</surname>
</row>
</documentElement>
My Example Where Selector
$where = "Jack";
$xml = "example.xml";
$sxml = simplexml_load_string($xml);
foreach ($sxml->row as $data=>$row)
{
if ($where == $data->name) // some code here
else // other some code here
}
Please let me know.
Thank you.
Yes, there is a way: XPath
$where = "Jack";
$xml = "example.xml";
$sxml = simplexml_load_string($xml);
var_dump($sxml->xpath('/documentElement/row/name[.="'.$where.'"]/..'));
No, but you can do this:
$where = "Jack";
$xml = "example.xml";
$sxml = simplexml_load_string($xml);
foreach ($sxml->row as $row)
{
if ($row->name == $where) {
// ...
} else {
// other some code here
}
}

retrieve atom:id from XML

How do you retrieve the value in atom:id from a XML document?
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">
<atom:id>http://www.google.com/m8/feeds/profiles/domain/mydomain.com/full/test</atom:id>
</atom:entry>
You can use SimpleXML and XPath for that:
$xml = <<<XML
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">
<atom:id>http://www.google.com/m8/feeds/profiles/domain/mydomain.com/full/test</atom:id>
</atom:entry>
XML;
$xml = new SimpleXMLElement($xml);
$result = $xml->xpath('/atom:entry/atom:id');
foreach ($result as $curResult)
{
echo __FILE__ . ':' . __LINE__ . '<pre>' . print_r($curResult, 1) . '</pre>';
}
You could use simple_xml and an xpath query. Like so:
$xml = <<<EOF
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">
<atom:id>http://www.google.com/m8/feeds/profiles/domain/mydomain.com/full/test</atom:id>
</atom:entry>
EOF;
$doc = simplexml_load_string($xml);
$el = $doc->xpath('//atom:id');
echo (string)$el[0];
(obviously that's without error checking and all)

Categories