繁体   English   中英

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

[英]PHP How to load file contents as XML variable

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

这是我的txt文件(userlist.txt)的输出abc xyz 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;
 }
?>

下面是输出

 <?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元素不是内联的,我不知道该怎么做。

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

file('userlist.txt', FILE_IGNORE_NEW_LINES)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM