Ok so I have some XML data.
$mydata = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE fmresultset PUBLIC "-//FMI//DTD fmresultset//EN" "https://HIDDEN:443/fmi/xml/fmresultset.dtd">
<fmresultset xmlns="http://www.filemaker.com/xml/fmresultset" version="1.0">
<resultset count="1" fetch-size="1">
<record mod-id="27" record-id="754">
<field name="a_Constant_c">
<data>1</data>
</field>
<field name="a_Sch_ID_pk">
<data>100060</data>
</field>
<field name="a_SchoolHead_pk">
<data>100060_1</data>
</field>
<field name="b___Data_____________">
<data/>
</field>
<field name="b_1Name_School_Code_t">
<data>PJA</data>
</field>
<field name="b_1Name_School_t">
<data>Palmetto</data>
</field>
<field name="b_1Name_SchoolHead_t">
<data>John Doe</data>
</field>
<field name="b_Ad_Address1_t">
<data/>
</field>
<field name="b_Ad_Address2_t">
<data>123 Main St.</data>
</record>
</resultset>
</fmresultset>
XML;
Now what I want to do is basically be able to read the value of the data from a specific field and assign it to a variable.
So far I have something like this...
$xml = simplexml_load_string($mydata);
Now I want to be able to assign let's say the data in the field name b_1Name_School_Code_t (which is PJA)
So I think it should be something like this
$school = $xml->resultset->record->field->data;
echo "School Name: ".$school;
Then I would like to see on the screen
School Name: PJA
So what I am missing to be able to make this happen?
You are only getting to the first field element in your example, which is why you get 1. Instead, loop through all the field elements, and stop when you get to the one you need:
$xml = simplexml_load_string($mydata);
$fields = $xml->resultset->record->field;
foreach ($fields as $field) {
if ((string) $field->attributes()->name === "b_1Name_School_Code_t") {
echo "School name: ".$field->data; // School name: PJA
break;
}
}
Demo
I use SimpleXMLElement::attributes() to get the name attribute of the element (note the cast to string, otherwise you get an SimpleXMLElement)
However, it would make more sense to use XPath to go directly to the element you're after:
$xml = simplexml_load_string($mydata);
$xml->registerXPathNamespace("fmresultset", "http://www.filemaker.com/xml/fmresultset");
$node = $xml->xpath("//fmresultset:resultset/fmresultset:record/fmresultset:field[#name='b_1Name_School_Code_t']");
var_dump($node[0]->data); // PJA
Demo
Notice the namespace registration and the accessing of the first element, since xpath() returns an array of SimpleXMLElements
Related
I have gone through a lot of the SimpleXML questions on this site. My data is a bit strange and I cannot change that. I am trying to get things like 'Building1' and 'Hostname1' from my data, so I can take that data and look up other data, then display it.
Here is a sample of my data:
<?xml version='1.0' encoding='UTF-8'?>
<results preview = '0'>
<result offset='0'>
<field k='hostname'>
<value h='1'><text>Hostname 1</text></value>
</field>
<field k='os'>
<value><text>Windows 7</text></value>
</field>
<field k='location'>
<value h='1'><text>Building 1</text></value>
<field>
</result>
<result offset='1'>
<field k='hostname'>
<value h='1'><text>Hostname 2</text></value>
</field>
<field k='os'>
<value><text>Windows 10</text></value>
</field>
<field k='location'>
<value h='1'><text>Building 2</text></value>
</field>
</result>
........
And here is how I am trying to look at it:
$xml = simplexml_load_file(data.xml);
print_r($xml);
$testArray = new SimpleXMLElement($xml);
$records = $testArray->results->result;
print_r($records);
For some reason I just cannot figure out how to get the data from the xml elements. If anyone can point me in the right direction, I'd appreciate it. I've tried many, many options. Thanks-
This is a really common mistake, but a really hard one to spot if you don't know what you're looking for: the first object you get back when parsing with XML is the root element, not something representing the document.
So in your case, $testArray is the element <results preview = '0'>, and you want $testArray->result not $testArray->results->result.
By the way, "testArray" is a bad name for this variable - it's not an array, it's an object.
I used xml as string in file
<?php
$sXmlString = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<results preview = "0">
<result offset="0">
<field k="hostname">
<value h="1"><text>Hostname 1</text></value>
</field>
<field k="os">
<value><text>Windows 7</text></value>
</field>
<field k="location">
<value h="1"><text>Building 1</text></value>
</field>
</result>
<result offset="1">
<field k="hostname">
<value h="1"><text>Hostname 2</text></value>
</field>
<field k="os">
<value><text>Windows 10</text></value>
</field>
<field k="location">
<value h="1"><text>Building 2</text></value>
</field>
</result>
</results>
EOF;
echo '<pre>';
$xml = simplexml_load_string($sXmlString);
print_r($xml);
echo '<hr/>';
echo count($xml->result);
echo '<hr/>';
foreach($xml->result as $report)
{
var_dump($report);
echo '<hr/>';
}
In the code you can see $xml it self reference to the "results" (or root) element.
You need to travel from the root to child elements. $xml->result will give the result object in the results set and you need to go for loop as it as array of objects.
I feel like this should be easy, but for the love of it I cannot get it right. I've read and tested for hours, trying different approaches I have found on the interwebs, both with simpleXML and PHP XML DOM, but none of them works just the way I need.
I got somewhat close, after tweaking and combining a few answers (that I cannot even find the way back to cause I've been through so many of them here), but not entirely correct.
My code:
$xml = simplexml_load_file(all_objects.xml");
$image = $xml->xpath('/objects/object/images/image');
foreach($image as $node) {
$id = (string) $node->field[0];
$url = (string) $node->field[4];
echo $id . " : " . $url . "<br>";
}
But instead of using the key/number they appear [4] in, I would like to target using the field name attribute, e.g. "id" and "image_url", as they are not always in this order.
Something like this:
$id = (string) $node->field["id"];
But it does not work, I tried with field->attribtues()->id too, but no luck.
The xml:
<objects>
<object>
<field name="id">1055</field>
<field name="title">example object</field>
<images>
<image number="1">
<field name="id">55</field>
<field name="version">1</field>
<field name="image_url_small">http://example.com/image-small.jpg</field>
<field name="image_url_medium">http://example.com/image-medium.jpg</field>
<field name="image_url_big">http://example.com/image-big.jpg</field>
<field name="image_url_original">http://example.com/image.jpg</field>
</image>
<image number="2">
<field name="id">56</field>
<field name="version">2</field>
<field name="image_url">http://example.com/image2.jpg</field>
</image>
<image number="3">...</image>
...
<image number="25">...</image>
</images>
</object>
<object>...</object>
<object>...</object>
</objects>
I would really appreciate any help/guidance! I am losing my mind over this.
You can use XPath to get child element with certain attribute value :
foreach($image as $node) {
$id = (string) $node->xpath('field[#name="id"]')[0];
$url = (string) $node->xpath('field[#name="image_url_big"]')[0];
echo $id . " : " . $url . "<br>";
}
eval.in demo
output :
55 : http://example.com/image-big.jpg
56 :
:
:
Hello and thank you for your help,
I am using PHP to write an XML request, the problem I am having is when I use the variable in the value field, it returns an error. However when I write in the value manually it works perfectly.
Under field name='Serial_Number' you will see the $MREPSerial is the variable, let's assume that
in the PHP we have $MREPSerial = 'A-000-1042'; The below XML would give an error. However if i were to replace $MREPSerial with just the value in the XML it would be successful. Any help would be greatly appreciated. Thank you!
$MREPSerial = htmlspecialchars(strtoupper($_POST['NSMREP']));
echo "Hi".$MREPSerial;
<ZohoCreator>
<applicationlist>
<application name='ajout-de-materiel'>
<formlist>
<form name='MREP'>
<update>
<criteria>
<field name='Serial_Number' compOperator='Equals' value={$MREPSerial}></field>
<reloperator>AND</reloperator>
<field name='MREP_Type' compOperator='Equals' value='0'></field>
</criteria>
<newvalues>
<field name='Is_being_Used' value='TRUE'></field>
</newvalues>
</update>
</form>
</formlist>
</application>
</applicationlist>
</ZohoCreator>";
...
the return response on echo (including the XML i echoed)
A-000-1012HI! <?xml version="1.0" encoding="UTF-8" ?>
<response><errorlist><error><code>2830</code><message><![CDATA[Open quote is expected for attribute "value" associated with an element type "field".]]></message></error></errorlist></response>
the return response on echo if i change it to '".$MREPSerial." is:
A-000-1012HI! <?xml version="1.0" encoding="UTF-8" ?>
<response><result><form name="MREP"><update><criteria><field name="Serial_Number" compOperator="Equals" value=""></field><reloperator>AND</reloperator><field name="MREP_Type" compOperator="Equals" value="0"></field></criteria><newvalues><field name="Is_being_Used"><value><![CDATA[TRUE]]></value></field></newvalues> <status>Failure, No Records Found With Specified Criteria</status></update></form></result></response>
You need quotes around the value attribute's actual value, like this:
<field name='Serial_Number' compOperator='Equals' value='{$MREPSerial}'></field>
just saw your "; close tag...
try this for that line
<field name='Serial_Number' compOperator='Equals' value='".$MREPSerial."'></field>
I have some XML that looks like this
$xml_str = '<RESPONSE>
<FIELDS>
<FIELD KEY="A">1</FIELD>
<FIELD KEY="B">2</FIELD>
<FIELD KEY="C">3</FIELD>
<FIELD KEY="D">4</FIELD>
</FIELDS>
</RESPONSE>';
There is only ever going to be 1 "FIELDS" in the response. Is there a easy way I can put the "FIELD" elements in a array with the keys being the "KEY" and the value being the element value?
I could do this
$xml_data = simplexml_load_string($xml_str);
foreach ($xml_data->FIELDS->FIELD as $field) {
foreach ($field->attributes() as $a => $b) {
$array[$b] = $field[0];
}
}
But I'm wondering if there is a better way?
TIA
The XML, I have:
<?xml version="1.0" encoding="windows-1252" ?>
- <Tables>
- <Table name="I_BOOKING">
- <Row action="UPDATE" success="Y">
<Field name="AUTOBOOK">888800</Field>
<Field name="TOUROP">01</Field>
<Field name="REFERENCE">GSDFFD</Field>
<Field name="NBPAX">2</Field>
<Field name="NBINF">1</Field>
</Row>
</Table>
- <Table name="I_EXCDATERESA">
- <Row action="UPDATE" success="Y">
<Field name="EXCURS">KNO</Field>
<Field name="DATE">2012-04-12</Field>
<Field name="BOOKNR">125445</Field>
<Field name="NAME">TEST 12/4</Field>
<Field name="PICKUPTIME">00:00:00</Field>
</Row>
- <Row action="UPDATE" success="Y">
<Field name="EXCURS">KNO</Field>
<Field name="DATE">2012-04-13</Field>
<Field name="BOOKNR">14574575</Field>
<Field name="NAME">TEST 13/4</Field>
<Field name="PICKUPTIME">00:00:00</Field>
</Row>
</Table>
</Tables>
When I treat the table I_EXCDATERESA, I need to get the value of Field BOOKNR, so 125445 or 14574575 in this example, according to the row I am dealing with and load it in $autobook:
...
$simplexml = simplexml_import_dom($dom);
foreach ($simplexml->Table as $value)
{
$tableName = $value->attributes()->name;
foreach ($value->Row as $value)
{
if ($tableName == 'I_EXCDATERESA')
{
if ($value->Field->attributes()->name == 'BOOKNR')
{
$autoBook = $value->Field;
This is not working, $autobook is not loaded, as it is not on the first 'Field' but on the third
This is a great use case for XPath:
$search = $simplexml->xpath('/Tables/Table[name="I_EXCDATERESA"]/Row/Field[name="BOOKNR"]');
Test this, it worked for me:
foreach ($simplexml->Table as $value)
{
$tableName = $value->attributes()->name;
foreach ($value->Row as $value_1)
{
if ($tableName == 'I_EXCDATERESA')
{
foreach ($value_1->Field as $value_2)
{
if ($value_2->attributes()->name == 'BOOKNR')
{
$autoBook = $value_2[0];
echo $autoBook;
}
}
}
}
}
Your problem is with:
$value->Field->attributes()->name=='BOOKNR'
That's too much: I added another cycle on each field of elemts Row!
well there are couple of ways to approach this situation
using DOM
using the attribute() function inside simplexml which was covered here
and probably the easiest option is noticing that BOOKNER is the third node in every Row
I would do something like this.
for ($i = 0; $i < count($simplexml->->children()); $i++){
echo $simplexml->Table[$i]->Table->Field[2];
}
good luck
$simplexml->xpath('/Tables/Table[name="I_EXCDATERESA"]/Row/Field[name="BOOKNR"]');
does'nt work, you missed the #
$simplexml->xpath('/Tables/Table[#name="I_EXCDATERESA"]/Row/Field[#name="BOOKNR"]');