我正在使用以下调用来获取部分DOM的XML代码。
var sXML = new XMLSerializer().serializeToString(document.getElementsByTagName("TopElementTag")[0]);
但是,当我显示这个字符串时,它就是一行。
有没有办法格式化这个字符串,以便它有换行符和标签,使其易于阅读?
答案 0 :(得分:7)
我使用以下代码vkBeutify。
var sXML = new XMLSerializer().serializeToString(document.getElementsByTagName("TopElementTag")[0]);
sXML = vkbeautify.xml(sXML);
答案 1 :(得分:0)
这个小型图书馆可以美化和缩小小于1kb的未压缩文件。
https://gist.github.com/max-pub/a5c15b7831bbfaba7ad13acefc3d0781
https://codepen.io/maxpub/pen/qBWPJEL?editors=1010
data = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"title": "ABC",
"startDate": 1100,
"endDate": 1200,
"latitude": 60.814,
"longitude": 11.845,
"content": "content."
},
"geometry": {
"type": "Point",
"coordinates": [60.814, 11.845, 1]
}
},
{
"type": "Feature",
"properties": {
"title": "XYZ",
"startDate": 1100,
"endDate": 1200,
"latitude": 40.814,
"longitude": 15.845,
"content": "content."
},
"geometry": {
"type": "Point",
"coordinates": [40.814, 15.845, 1]
}
},
]
}
mapdata2 = {
"type": "FeatureCollection",
"features": []
};
data.features.forEach((feature) => {
mapdata2.features.push({
type: "Feature",
properties: {
title: feature.properties.title,
startDate: feature.properties.startDate,
endDate: feature.properties.endDate,
id: feature.properties.id,
latitude: feature.properties.latitude,
longitude: feature.properties.longitude,
content: feature.properties.content
},
geometry: {
type: "Point",
coordinates: feature.geometry.coordinates
}
})
});
console.log(mapdata2);