How to get the valid XML notation in php from DB - php

I have stored below mention XML in a table(MS-SQL DB).
<CUSTOMER>
<CUSTOMERDATA>
<USERID>12691</USERID>
<USERCODE>FFRD991</USERCODE>
<MOBILENO>5645353443</MOBILENO>
<EMAILID>jhfghfghgf#sdf.fh</EMAILID>
<FIRSTNAME>ggdg</FIRSTNAME>
<MIDDLENAME>dfgdfgdf</MIDDLENAME>
<LASTNAME>gdfgdf</LASTNAME>
<ADDRESS></ADDRESS>
<CITY></CITY>
<PINCODE></PINCODE>
<STATENAME></STATENAME>
<SOURCE>Others</SOURCE>
<CREATEDATE>2015-12-01</CREATEDATE>
<STATUS></STATUS>
</CUSTOMERDATA>
</CUSTOMER>
Now I want to fetch this record by php using the following code :
$query = "Select xml_rec from tbl_node where UserId = 12691";
$result = sqlsrv_query($this->db->conn_id, $query);
$row = sqlsrv_fetch_object($result);
//// OR
$row = sqlsrv_fetch_array($result , SQLSRV_FETCH_ASSOC);
echo $row['xml_rec'];
I am getting the following wrong XML . Please note the blank tags.
<CUSTOMER>
<CUSTOMERDATA>
<USERID>12691</USERID>
<USERCODE>FFRD991</USERCODE>
<MOBILENO>5645353443</MOBILENO>
<EMAILID>jhfghfghgf#sdf.fh</EMAILID>
<FIRSTNAME>ggdg</FIRSTNAME>
<MIDDLENAME>dfgdfgdf</MIDDLENAME>
<LASTNAME>gdfgdf</LASTNAME>
<ADDRESS/>
<CITY/>
<PINCODE/>
<STATENAME/>
<SOURCE>Others</SOURCE>
<CREATEDATE>2015-12-01</CREATEDATE>
<STATUS/>
</CUSTOMERDATA>
</CUSTOMER>
How to fix this to show the proper XML ? Thanks for your valuable time.

You don't really use any xml-processing functions here, you simply retrieve a string and display it. The most likely cause of your problem was that some time when you stored this data in your DB, the empty tags got processed from the
<tag></tag>
form into
<tag />
form.
You can check this by connecting to your DB directly (without PHP) and running the query manually.
In any case, as lad2025 pointed out, the two ways of writing empty tags are equivalent so it shouldn't matter for your application.

Related

retrieving an xml file from mysql and using it in as a variable with php

I'm retrieving an XML code from the database and storing it as a variable in php. However when I try to use this variable inside an XML code, it gives me an error. Here is my code:
<?php
$conn = mysqli_connect('localhost', 'root', '', 'thisdatabase');
$result = mysqli_query($conn, 'SELECT * FROM createdproduct');
while ($row = mysqli_fetch_assoc($result))
{
//$selected = (isset($_POST['list']) && $_POST['list'] == $row['id']) ? 'selected' : '';
//echo htmlentities($row["productURL"]);
$test = htmlspecialchars($row["productURL"]);
}
//echo htmlentities($test);
?>
and this is my XML wrapped in php:
<?php
//some code
$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.mydomain.net">
<orderItems>
'.$test.'
</orderItems>
<payment>
<type>typ</type>
</payment>
<shipping>
<shippingType id="00"/>
<address type="private">
<person> <
<salution id="1"/>
<firstName>person</firstName>
<lastName>person</lastName>
</person>
<street>street</street>
<houseNumber>00</houseNumber>
<city>city</city>
<country code="US">USA</country>
<state code="mm">California</state>
<zipCode>111</zipCode>
<email>aaa#mydomain.net</email>
<phone>+49 341 789 123</phone>
<fax>+49 341 789 123</fax>
</address>
</shipping>
</order>';
//some code
?>
Your error code is rather generic but I suspect the issue is in the data you're pulling from the database and using in the $test variable.
As discussed above, you might try confirming this by using a simple word in place of the actual data, like: $test = 'something';. The reason I believe this to be the problem is because htmlentities() is intended for use in HTML, not XML.
You might consider trying htmlspecialchars() instead.
EDIT: I knew I had read this on SO somewhere, check this post out for further detailed information.
I was able to get the result I wanted using html_entity_decode. Thank you

Turning XML nodes into PHP variables using SimpleXML on a relatively complex XML file

I am using simplexml_load_string in order to attempt to turn my XML file into variables I can later enter into a database however am struggling with the output working in some instances and not in others.
The xml
$response ='<ndxml version="2.0">
<status code="OK">
<response function="createNewJob" id="1">
<status code="OK">
<job uniqueref="5830z858279" jobref="858279">
<consignment number="8613030">
<reference>16755</reference>
<deadlinedatetime date="2014-01-16" time="17:30:00">
<jobnumber>
<labeldata styletag="APC">
<shippingdate date="15/01/2014">
<addresses>
<address type="COLLECTION">
<company>UK Stuff and Things</company>
</address>
<address type="DELIVERY">
<contact>Person</contact>
<telephone>02089636985</telephone>
<addresslines>
<addressline number="1">Daffy</addressline>
<addressline number="2">Things</addressline>
<addressline number="3">Places</addressline>
<addressline number="4">NORTHAMPTONSHIRE</addressline>
</addresslines>
<postzip>NB12 1ER</postzip>
<country isocode="GB">United Kingdom</country>
</address>
</addresses>
<notes>
<account code="21171">
<tariff code="MP16">
<routing>
<delivery>
<route>LOCAL</route>
<zone>B</zone>
<driver>31</driver>
<serviceoptions>
</serviceoptions></delivery>
<depots>
<depot number="211" type="Sending">
<depot number="211" type="Request">
<depot number="211" type="Delivery">
</depot></depot></depot></depots>
</routing>
<parcels total="1">
<dimensions height="0" width="0" length="0">
<volweight>0.0</volweight>
<weight>0.14</weight>
<parcel number="1">
<barcode>21163148613030001</barcode>
</parcel>
</dimensions></parcels>
</tariff></account></notes></shippingdate></labeldata>
</jobnumber></deadlinedatetime></consignment></job>
</status>
</response>
So I have managed to successfully grab certain elements from this by using the recommended code on the documentation:
$parsed=simplexml_load_string($response);
$response_statuscode = $parsed->status['code'];
$response_statuscode2 = $parsed->response->status['code'];
$response_consignment_num = $parsed->response->job->consignment['number'];
$response_reference = $parsed->response->job->reference;
All of these have worked exactly as required, however from there it all goes a bit wrong for me. Things with more complicated attributes (more than one!) just don't seem to be working for me.
$response_date = $parsed->response->job->deadlinedatetime['date'];
I also tried:
$parsed->response->job->deadlinedatetime->attributes()->date;
And from there on I can't seem to process anything from label data properly. I am just making a mess of my understanding of the tree?
$response_account_code = $parsed->response->job->labeldata->account['code'];
As always, thanks in advance!
There's a very common approach to handle situations like this one:
Since simplexml_load_string() returns an object and since object property names cannot contain spaces, it would make sense to recursively convert an object into array.
You can use this function to do that:
function object2array($object) {
return json_decode(json_encode($object), true);
}
$parsed = simplexml_load_string($response);
// Now, recursively convert it into an array
$parsed = object2array($parsed);
// Now you can access its values by keys, like this:
$parsed['response']['job']['labeldata']['account'];
As for dumping, you can simply do print_r($parsed)

Show all items that Match in XML using PHP

This is my XML file named: full.xml
I need your help. I need a PHP script that open "full.xml"
and only display all values of the nodes that have .email
Example of the Output I want:
sales#company1.com
sales#company2.com
sales#company3.com
Thanks! I will thank you so much!
EDIT
$Connect = simplexml_load_file("full.xml");
return $Connect->table[0]->*.email;
The design of your XML is not very smart. With this xpath expression, you select all nodes with .email at the end of their name:
$xml = simplexml_load_string($x); // assume XML in $x
$results = $xml->xpath("//*[substring(name(),string-length(name())-" . (strlen('.email') - 1) . ") = '.email']");
--> result is an array with the selected nodes.
BTW: if you have any chance of CHANGING the structure of the XML, AVOID combining information within node names like <company1.email>, but do it like this:
...
<companies>
<company id="1">
<email>info#company1.com</email>
<tel>+498988123456</tel>
<name>somename</name>
</company>
<company id="2">
<email>info#company2.com</email>
<tel>+498988123457</tel>
<name>someothername</name>
</company>
</companies>
....
It will be much easier to read and parse.

inserting data into mysql database through xml

while i am inserting data into database using normal xml tree structure data is successfully inserted but while i am trying to insert data into database using different xml structure it is not gives me any error but at the same time fields are created but i can't visualize content in table to,
<?xml version="1.0"?>
<xml>
<draw>
<candelete>yes</candelete>
<forpayroll>no</forpayroll>
<name>hello</name>
</draw>
</xml>
above xml format which successfully insert data into mysql database
below xml format is not allowing me to insert data into mysql database
<ENVELOPE>
<HEADER>
<VERSION>1</VERSION>
<STATUS>1</STATUS>
</HEADER>
<BODY>
<DESC>
</DESC>
<DATA>
<TALLYMESSAGE>
<LEDGER NAME="Dena" RESERVEDNAME="" ID="2240" REQNAME="dena">
<PARENT TYPE="String">Bank Accounts</PARENT>
<TAXTYPE TYPE="String">Others</TAXTYPE>
<ISBILLWISEON TYPE="Logical">No</ISBILLWISEON>
<ISCOSTCENTRESON TYPE="Logical">No</ISCOSTCENTRESON>
<ISREVENUE TYPE="Logical">No</ISREVENUE>
<ISDEEMEDPOSITIVE TYPE="Logical">Yes</ISDEEMEDPOSITIVE>
<CANDELETE TYPE="Logical">Yes</CANDELETE>
<FORPAYROLL TYPE="Logical">No</FORPAYROLL>
<MASTERID TYPE="Number"> 2240</MASTERID>
<TNETBALANCE TYPE="Amount">0.00</TNETBALANCE>
<LANGUAGENAME.LIST>
<NAME.LIST TYPE="String">
<NAME>Dena</NAME>
</NAME.LIST>
<LANGUAGEID TYPE="Number">0</LANGUAGEID>
</LANGUAGENAME.LIST>
</LEDGER>
</TALLYMESSAGE>
</DATA>
</BODY>
</ENVELOPE>
below is my php insert connection code
<?php
$con = mysql_connect("localhost:3306","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("temp", $con);
if(!$xml=simplexml_load_file('./xml/data.xml')){
trigger_error('Error reading XML file',E_USER_ERROR);
}
foreach ($xml as $syn)
{
$candelete = $syn->candelete;
$forpayroll = $syn->forpayroll;
$name = $syn->name;
$sql = "INSERT INTO vtiger (candelete, forpayroll, name) VALUES ('$candelete','$forpayroll','$name')";
$query = mysql_query($sql);
if (!$query)
{
echo ('Error: ' . mysql_error());
}
else
{
echo "Record added";
}
}
mysql_close($con);
?>
I'd like you to look at your foreach loop to read xml:
foreach ($xml as $syn)
{
$candelete = $syn->candelete;
$forpayroll = $syn->forpayroll;
$name = $syn->name;
as you see here you decleare xml nodes wich are totally different in new xml, so you need to parse your document and get all relevant data to build the query. A good approch would be to loop among <TALLYMESSAGE> to retrieve all relevant data and store into variables wich you will be able to use for the new query.
You can use simplexml_load_file to do that.
Documentation: here
NOTE You query also must be changed according with new xml, you will need a new table with a correct schema.
UPDATED
You can still use the same way to parse file but you need to change your script and you will end up with more lines of code to parse the entire new xml. As you can see structure is totally different hand have many child nodes wich cannot be retrieved by using a simple foreach as you did for the first file wich didn't have any child nodes. That's why you really should use simplexml_load_file, less work
You need to think more about your foreach loop, bcause in your working xml file thats only one array so you can retrieve data using only one foreach loop, but your another xml file i think it contains more array so you need to create one or more foreach loop according to its structure.
you define your conection to mysql in $con
you must also use mysql_query($sql, $con);

Getting single result from Simple XML Array

Ok i am using a simple XML function to get some information from a remote database, this works fine, however it returns all of the information in an xml format, and what i want to do is just show the results of one field, so for example below is the result i want to display
<product id="9" name="Computer screwdriver" datasheet="" packshot=""/>
and below is my code
$file = 'http://computers.mysite.co.uk/vrm.xml?apikey=**********&vid=Check&vrm='.$reg;
if(!$xml = simplexml_load_file($file))
exit('Failed to open '.$file);
print_r($id);
UPDATE
ok so i have now changed my code to this:
$data = simplexml_load_file("http://test.mysite.co.uk/test.xml? apikey=********&vid=app&vrm='.$reg;");
print $data->computers->computer->products->product->name;
The xml structure is here:
<computers sid="1234">
<computer id="253406" name="computer name)" model_group="Microsofth" start_year="2005" end_year="2009">
<system id="969623" capacity="3.4"/>
<developer id="64" name="intel"/>
<machine id="8" name="P" etype="P"/>
<products>
<product id="9" name="computer screwdriver" datasheet="" packshot=""/>
<product id="16" name="Screwdriver Crosshead" datasheet="" packshot=""/>
</products>
</computer>
</computers
What i want to do is to return the name of the product in the first line of products only, in this case id = 9.
At the moment the way the code isn't showing any results though if i change it to show all results it does, so the problem is just trying to filter that one result out.
Any help would be appreciated.
Thanks
$data->xpath("you_selector")
and don't forget
header("Content-Type:text/xml");

Categories