0

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.

4
  • Do you mean how do I remove the newline? Commented Jul 23, 2018 at 13:20
  • output should be <uname>abc</uname> like that Commented Jul 23, 2018 at 13:22
  • So then YES you do mean how do I remove the newline at the end of the line I read from the file. See @NigelRen answer Commented Jul 23, 2018 at 13:23
  • Or you could trim($line); if you prefer Commented Jul 23, 2018 at 13:24

1 Answer 1

2

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

file('userlist.txt', FILE_IGNORE_NEW_LINES)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.