3

DOMParser 在解析 xml 的时候,如果含有&符号,会失败

 2 years ago
source link: https://www.v2ex.com/t/833899
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  JavaScript

DOMParser 在解析 xml 的时候,如果含有&符号,会失败

  yazoox · 21 小时 34 分钟前 · 941 次点击
export function convertStringToDOM(strXml: string): Document {
  const parser = new DOMParser();
  const doc = parser.parseFromString(strXml, "application/xml");
  return doc;
}

这里的 strXml 比如,

<test clsid="g16A07941-BC15-4D48-A880-9D5A211D5065" ID="g16A07941-BC15-4D48-A880-9D5A211D5066">
    <Db clsid="g16A07941-BC15-4D48-A880-9D5A211D5093" ID="ga92">
        <Prop propname="db1">/a/b/c/d</Prop>
        <Prop propname="db1">1 & 2</Prop>
    </Db>
</test>

最近需要读取一些文件,展示在网页上面。都是 xml 的数据,使用 Javascript 的 DOMParser 解析这些文件的时候,总是会失败。 debug 了一下发现,是因为这些 xml 的一些内容,比如上面的“1 & 2”里面含有 & 这个符号。只要有这个符号,读取就会失败。DOMParser 不能识别或者智能解析这个&符号么?

大家有没有比较好的办法? xml 数据源这个没有办法,是客户数据。我们只能想办法读取出来。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK