2

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.

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
Abhi
  • 29
  • 2
  • 1
    `` and `` are equivalent. Check http://stackoverflow.com/questions/2279501/what-is-an-empty-element – Lukasz Szozda Dec 17 '15 at 13:38
  • Little ago I answered a similar question here: http://stackoverflow.com/a/32985478/5089204 You will find there a bunch of tricks how you could influence the way of handling empty elements. – Shnugo Dec 17 '15 at 13:54

1 Answers1

0

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.

Alex Kirko
  • 446
  • 3
  • 8