function encodeXML(s) {
const dom = document.createElement('div')
dom.textContent = s
return dom.innerHTML
}
<?xml version="1.0" encoding="UTF-8"?>
<animals class="object">
<dog class="array">
<e class="object">
<age type="number">15</age>
<name type="string">Rufus</name>
</e>
<e class="object">
<age class="object" null="true"/>
<name type="string">Marty</name>
</e>
</dog>
</animals>
<?xml version="1.0" encoding="UTF-8"?>
<animals class="object">
<dog class="array">
<e class="object">
<age type="number">15</age>
<name type="string">Rufus</name>
</e>
<e class="object">
<age class="object" null="true"/>
<name type="string">Marty</name>
</e>
</dog>
</animals>
如果需要对XML、HTML进行格式化的可以看这个文章:https://blog.csdn.net/qq_38132283/article/details/106568063