DOMParser解析xml字符串

如题,实现如下:

var parser = new DOMParser()
var result = parser.parseFromString('' +

  '' +
  '' +
  '    ' +
  '        frames' +
  '        ' +
  '            area_partner_1.png' +
  '            ' +
  '                aliases' +
  '                ' +
  '                anchor' +
  '                {0.5,0.5}' +
  '                spriteOffset' +
  '                {0,0}' +
  '                spriteSize' +
  '                {678,641}' +
  '                spriteSourceSize' +
  '                {678,641}' +
  '                textureRect' +
  '                {{1,1},{678,641}}' +
  '                textureRotated' +
  '                ' +
  '            ' +
  '        ' +
  '        metadata' +
  '        ' +
  '            format' +
  '            3' +
  '            pixelFormat' +
  '            RGBA8888' +
  '            premultiplyAlpha' +
  '            ' +
  '            realTextureFileName' +
  '            wqer.png' +
  '            size' +
  '            {846,643}' +
  '            smartupdate' +
  '            $TexturePacker:SmartUpdate:0d31322d15aeee6aaabcfc82cc4676df:91776dda71b0a5779c04559a94ab5066:01c2a16656db932f59fe35247e90d956$' +
  '            textureFileName' +
  '            wqer.png' +
  '        ' +
  '    ' +
  '', '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)

你可能感兴趣的:(DOMParser解析xml字符串)