user2713122
user2713122

Reputation: 11

PHP How to load file contents as XML variable

I have a requirement to load the variable from txt file to the particular XML element. but some how I am not able to echoing properly.

Here is my output of txt file (userlist.txt) abc xyz LMN

and below is code .

<?php
           foreach(file('userlist.txt') as $line) {
          $xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
          <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
           xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                <soap:Body>
                <Getid xmlns="http://test_xyz/">
                <uname>'. $line. '</uname>
                 <org>Test_GRP</org>
               </Getid>
             </soap:Body>
        </soap:Envelope>';
       echo $xml_post_string;
 }
?>

and below is OUTPUT

 <?xml version="1.0" encoding="utf-8"?>
              <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
               <soap:Body>
                <Getid xmlns="http://test_xyz/">
               <uname>abc
             </uname>
              <org>Test_GRP</org>
            </Getid>
           </soap:Body>
          </soap:Envelope>

the uname element is not inline, any idea how I do that.

Upvotes: 0

Views: 41

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57141

You need to flag file() to ignore the new lines at the end of each line...

file('userlist.txt', FILE_IGNORE_NEW_LINES)

Upvotes: 2

Related Questions