Pulling a specific field with php out of a xml upload - php

I've been trying to get the id field to pull and have no idea where I'm going wrong. The rest of the data pulls correctly but I'm trying to add something new to some existing code and everything I've tried hasn't worked. Below is my XML and the PHP code I've been working off of.
I haven't worked with a combo of xml and php before so I could really use a push in the right direction.
<?xml version="1.0" encoding="UTF-8"?>
<enterprise>
<person>
<sourcedid>
<source>Spirit Awards</source>
<id>SP8675309</id>
</sourcedid>
<userid>...</userid>
<name>
<fn>...</fn>
</name>
<email>...</email>
</person>
PHP code:
function get_userid(){
return $this->uid;
}
function __construct($xmlData){
$this->uid = (string)$xmlData->id;
}

SimpleXMLdocs makes this ... well ... simple. The XML you've posted is missing a closing </enterprise> tag. Assuming the XML you're actually parsing includes the closing tag, consider:
$str = '<?xml version="1.0" encoding="UTF-8"?>
<enterprise>
<person>
<sourcedid>
<source>Spirit Awards</source>
<id>SP8675309</id>
</sourcedid>
<userid>...</userid>
<name>
<fn>...</fn>
</name>
<email>...</email>
</person>
</enterprise>
';
$xml = simplexml_load_string($str);
$var = (string) $xml->person->sourcedid->id;
echo $var; // outputs: SP8675309

Related

PHP get specific data from xml file

This is XML file.
<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<UWdata>
<List>
<IdProduct>1</IdProduct>
<ProductName>product</ProductName>
<ProductNameDE>product</ProductNameDE>
<ProductNameEN>product</ProductNameEN>
<Uf>1</Uf>
<PSIg>1</PSIg>
<Ug>1</Ug>
</List>
</UWdata>
</UW>
$lines_array=file($url);
$lines_string=implode('',$lines_array);
$xml=simplexml_load_string($lines_string) or die("Error: Cannot create object");
I try with this
echo $xml->UWdata[1]->ProductName;
But it doesn't return anything.I want to return Product name.
Sample code, Use simplexml_load_string
<?php
$a = '<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<UWdata>
<List>
<IdProduct>1</IdProduct>
<ProductName>product</ProductName>
<ProductNameDE>product</ProductNameDE>
<ProductNameEN>product</ProductNameEN>
<Uf>1</Uf>
<PSIg>1</PSIg>
<Ug>1</Ug>
</List>
</UWdata>
</UW>';
$xml=simplexml_load_string($a) or die("Error: Cannot create object");
echo ($xml->UWdata->List->ProductName);
?>
When you load the xml file using the php simplexml_load_file function to a variable. The veritable becomes an object.
<?php
$xml=simplexml_load_file("/path/to/the/file.xml");
?>
So, in your case, the $xml variable becomes a multi-level object where every elements of xml file are key of the object. Like: UWdata.
So, as $xml is a multi-level object, to access the element under UWdata, under List under ProductName, you have to code like bellow.
echo $xml->UWdata->List->ProductName."<br>";
Here,
UWdata is the key of $xml object.
List is the key of UWdata.
ProductName is the key of List.
Finally, you will get the value of key element ProductName = product
I modified your script and put the xml in an external file called testxml.xml, as it should be. Always separate the function and the data it's supposed to handle. I used your xml like this:
<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<UWdata>
<List>
<IdProduct>1</IdProduct>
<ProductName>productTEST</ProductName>
<ProductNameDE>product</ProductNameDE>
<ProductNameEN>product</ProductNameEN>
<Uf>1</Uf>
<PSIg>1</PSIg>
<Ug>1</Ug>
</List>
</UWdata>
</UW>
And with the following script it returns productTEST only.
$xmlstr = file_get_contents('./testxml.xml');
$xml = simplexml_load_string($xmlstr);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
echo $array['UWdata']['List']['ProductName'];
Hope this helps.
//edit:
While I do not know your project, you might want to take a foreach-approach if it is possible for your xml to contain more than one List element

Nod added into xml as child, wanted sibling (SimpleXMLElement)

I want to add Data2 as a child of Request, but instead it gets added as a child of Data.
class xml{
public function __construct(){
$this->request_xml = new SimpleXMLElement("<Request></Request>");
$this->request_xml->addAttribute('RequestType', "1");
$this->request_xml->addChild("Data");
$this->request_xml->addChild("Data2");
var_dump($this->request_xml->asXml());
}
}
$object = new xml();
the result is:
<request>
<data>
<data2></data2>
</data>
</request>
I want
<request>
<data></data>
<data2></data2>
</request>
What am I missing?
Thanks!
The XML output is:
<?xml version="1.0"?>
<Request RequestType="1"><Data/><Data2/></Request>
In other words the Data and Data2 elements are siblings, but short empty tags. If the browser loads it as HTML it will try to repair the missing closing tags. This will not happen if it is parsed as XML. Make sure that you send the correct content type header:
header('Content-type: application/xml; charset=utf-8');
If you import the SimpleXMLElements into DOM (or better generate the document using DOM in the first place) you get a more options for saving the XML.
$element = dom_import_simplexml($request_xml);
echo $element->ownerDocument->saveXml(NULL, LIBXML_NOEMPTYTAG);
Output:
<?xml version="1.0"?>
<Request RequestType="1"><Data></Data><Data2></Data2></Request>

XML file creation using PHP [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
XML formatting is not working well
I am trying to create an XML file from the database. Database contains name, phone no and sex. I would like to get all the users' details in a well-formatted XML file. But I am getting now:
<CUSTOMERS>
<name>AAA</name>
<name>BBB</name>
</CUSTOMERS>
This is my code:
$xmlstr = "<?xml version='1.0' ?>\n"."<CUSTOMERS></CUSTOMERS>";
$xml = new SimpleXMLElement($xmlstr);
while($b=$result->fetch_assoc()){
$xml->addChild("name", $b['name']);
}
return $xml->asXML();
I would like to get the out put as shows below
<CUSTOMERS>
<AAAA>
<name>AAA</name>
<phone>111</phone>
<sex>male</sex>
</AAA>
<BBBB>
<name>BBB</name>
<phone>222</phone>
<sex>female</sex>
</AAA>
</CUSTOMERS>
Latest Code
$xmlstr = "<?xml version='1.0' ?>\n"."<CUSTOMERS></CUSTOMERS>";
$xml = new SimpleXMLElement($xmlstr);
while($b=$result->fetch_assoc()){
$customer = $xml->addChild("customer");
$customer->addChild("name", $b['name']);
$customer->addChild("phone", $b['phone']);
$customer->addChild("sex", $b['sex']);
//$xml->addChild("place", $b['place']);
}
return $xml->asXML();
Each customer should be in its own tag:
$d = simplexml_load_string('<?xml version="1.0" encoding="utf-8" ?><customers />');
$customer = $d->addChild('customer');
$customer->addChild('name', 'Jack');
// $customer->addChild('phone', '911');
// etc.
$customer = $d->addChild('customer');
$customer->addChild('name', 'John');
echo $d->asXML();
<?xml version="1.0" encoding="utf-8"?>
<customers>
<customer><name>Jack</name></customer>
<customer><name>John</name></customer>
</customers>
Note that the tag name should not be the name of your customer, it should rather be a description of what information is inside; hence my use of the customer tag.
You should be able to do it like this:
$xml = new SimpleXMLElement("<customers></customers>");
while($b=$result->fetch_assoc()){
$customer = $xml->addChild("customer");
$customer->addChild("name", $b['name']);
$customer->addChild("phone", $b['phone']);
$customer->addChild("sex", $b['sex']);
}
Output:
<customers>
<customer>
<name>AAA</name>
<phone>1234567</phone>
<sex>f</sex>
</customer>
</customers>
You are telling PHP to add the <name> child but nothing else.
$xml->addChild("name", $b['name']);
In that while loop, you need to add the <AAA> element, then the child <name>, <phone>, and <sex> (and etc) elements.
The question is to change this
$xmlstr = "<?xml version='1.0' ?>\n"."<CUSTOMERS></CUSTOMERS>";
to this
$xmlstr = = '<' . '?xml version='1.0' ?>\n"."<CUSTOMERS></CUSTOMERS>';
As the <? messes up the parser.
Also XML does not like capitals and needs a root - i.e. something between your customers.

Hide XML declaration in files generated using PHP

I was tesing with a simple example of how to display XML in browser using PHP and found this example which works good
<?php
$xml = new DOMDocument("1.0");
$root = $xml->createElement("data");
$xml->appendChild($root);
$id = $xml->createElement("id");
$idText = $xml->createTextNode('1');
$id->appendChild($idText);
$title = $xml->createElement("title");
$titleText = $xml->createTextNode('Valid');
$title->appendChild($titleText);
$book = $xml->createElement("book");
$book->appendChild($id);
$book->appendChild($title);
$root->appendChild($book);
$xml->formatOutput = true;
echo "<xmp>". $xml->saveXML() ."</xmp>";
$xml->save("mybooks.xml") or die("Error");
?>
It produces the following output:
<?xml version="1.0"?>
<data>
<book>
<id>1</id>
<title>Valid</title>
</book>
</data>
Now I have got two questions regarding how the output should look like.
The first line in the xml file '', should not be displayed, that is it should be hidden
How can I display the TextNode in the next line. In total I am exepecting an output in this fashion
<data>
<book>
<id>1</id>
<title>
Valid
</title>
</book>
</data>
Is that possible to get the desired output, if so how can I accomplish that.
Thanks
To skip the XML declaration you can use the result of saveXML on the root node:
$xml_content = $xml->saveXML($root);
file_put_contents("mybooks.xml", $xml_content) or die("cannot save XML");
Please note that saveXML(node) has a different output from saveXML().
First question:
here is my post where all usable threads with answers are listed: How do you exclude the XML prolog from output?
Second question:
I don't know of any PHP function that outputs text nodes like that.
You could:
read xml using DomDocument and save each node as string
iterate trough nodes
detect text nodes and add new lines to xml string manually
At the end you would have the same XML with text node values in new line:
<node>
some text data
</node>

AMFPHP & PHP - Return xml data with tags. Do not want

I'm using the simplexml extentsion and AMFPHP to send xml data to flash.
Say I have this xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<people>
<person>
<name>bob</name>
</person>
</people>
And I load it in with simplexml_load_file().
When I do this:
$name = $xml->person[0]->name;
return $name;
it returns "<name>bob</name>".
Why is it returning the tags? When I do this with just php and not AMFPHP it works fine.
I wanna know the answer to this too
Ok, i found out how to solve this :
You basically have to type it to a string, float, double, int, or whatever you need...
$xml = simplexml_load_file(XML_FILE_LOCATION);
$start = (double)$xml->layer[2]['start'];
//$title = (string)$xml->layer[2]['title'];
return $start;

Categories