【问题标题】:Turn XML into a Multiline String in Javascript在 Javascript 中将 XML 转换为多行字符串
【发布时间】:2019-05-28 15:11:59
【问题描述】:

我有一个 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<productenv:Envelope>
    <productenv:Body>
        <products>
            <productName>TestProduct</productName>
            <productPrice>50.00</productPrice>
        </products>
    </productenv:Body>
</productenv:Envelope>

我正在使用 PHP 从外部服务器接收此文件。然后将 XML 传递给 javascript 文件。以下是 javascript 对存储 XML 的变量所做的事情:

<script type="text/javascript">
  var result = "<?php echo $result; ?>"; // $result is the variable that sorts the xml file
  console.log(result);
</script>  

当我运行代码时,我得到这个错误:Uncaught SyntaxError: Unexpected number。这是因为 XML 被转换为字符串,但是 1.0 和 UTF-8 周围的双引号将文件弄乱了。

有什么方法可以使 XML 成为可以解析的多行字符串?

【问题讨论】:

标签: javascript xml string parsing


【解决方案1】:

假设$result字符串不包含任何反引号字符,您可以尝试使用javascript's template literals

<script type="text/javascript">
  var result = `<?php echo $result; ?>`; // $result is the variable that sorts the xml file
  console.log(result);
</script>  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-20
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多