PHP如何将文件内容加载为XML变量

时间:2018-07-23 13:18:41

标签: php xml

我需要将变量从txt文件加载到特定的XML元素。但是有些我无法正确地回声。

这是我的txt文件(userlist.txt)的输出 abc y LMN

和下面是代码。

<?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;
 }
?>

下面是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>

uname元素不是内联的,我不知道该怎么做。

1 个答案:

答案 0 :(得分:2)

您需要标记file(),以忽略每行末尾的新行...

file('userlist.txt', FILE_IGNORE_NEW_LINES)