乐趣区

关于html:DOMParser解析xml字符串

如题,实现如下:

var parser = new DOMParser()
var result = parser.parseFromString(‘<?xml version=”1.0″ encoding=”UTF-8″?>’ +

  '<!DOCTYPE plist PUBLIC"-//Apple Computer//DTD PLIST 1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">'+'<plist version="1.0">'+'    <dict>'+'        <key>frames</key>'+'        <dict>'+'            <key>area_partner_1.png</key>'+'            <dict>'+'                <key>aliases</key>'+'                <array/>'+'                <key>anchor</key>'+'                <string>{0.5,0.5}</string>'+'                <key>spriteOffset</key>'+'                <string>{0,0}</string>'+'                <key>spriteSize</key>'+'                <string>{678,641}</string>'+'                <key>spriteSourceSize</key>'+'                <string>{678,641}</string>'+'                <key>textureRect</key>'+'                <string>{{1,1},{678,641}}</string>'+'                <key>textureRotated</key>'+'                <false/>'+'            </dict>'+'        </dict>'+'        <key>metadata</key>'+'        <dict>'+'            <key>format</key>'+'            <integer>3</integer>'+'            <key>pixelFormat</key>'+'            <string>RGBA8888</string>'+'            <key>premultiplyAlpha</key>'+'            <false/>'+'            <key>realTextureFileName</key>'+'            <string>wqer.png</string>'+'            <key>size</key>'+'            <string>{846,643}</string>'+'            <key>smartupdate</key>'+'            <string>$TexturePacker:SmartUpdate:0d31322d15aeee6aaabcfc82cc4676df:91776dda71b0a5779c04559a94ab5066:01c2a16656db932f59fe35247e90d956$</string>'+'            <key>textureFileName</key>'+'            <string>wqer.png</string>'+'        </dict>'+'    </dict>'+'</plist>','application/xml')

console.log(result)
const keys = result.getElementsByTagName(‘key’)
const strings = result.getElementsByTagName(‘string’)

const arr = []
for (var i = 0; i < keys.length; i++) {

  if (/.*\.png/.test(keys[i].innerHTML)) {arr.push(keys[i])
  }

}
console.log(arr)

var arr1 = []
arr.forEach(item => {

  arr1.push(item.nextElementSibling.getElementsByTagName('key'))

})
console.log(arr1)

退出移动版