如何使用 javascript 或 jquery 读取 DBUnit XML
Posted
技术标签:
【中文标题】如何使用 javascript 或 jquery 读取 DBUnit XML【英文标题】:How to read DBUnit XML with javascript or jquery 【发布时间】:2021-03-14 00:04:28 【问题描述】:晚上好,我在尝试阅读这种 XML 时遇到问题。
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<MOCK_DATA id="1" first_name="Sandor" last_name="Balmer" email="sbalmer0@qq.com" gender="Male" ip_address="148.134.84.45"/>
<MOCK_DATA id="2" first_name="Val" last_name="Toll" email="vtoll1@tamu.edu" gender="Male" ip_address="249.13.225.19"/>
<MOCK_DATA id="3" first_name="Phyllida" last_name="Purrier" email="ppurrier2@vimeo.com" gender="Female" ip_address="156.189.251.161"/>
<MOCK_DATA id="4" first_name="Marjy" last_name="Pittam" email="mpittam3@businessinsider.com" gender="Female" ip_address="193.195.178.210"/>
<MOCK_DATA id="5" first_name="Kirbee" last_name="Regglar" email="kregglar4@unblog.fr" gender="Female" ip_address="251.218.161.174"/>
<MOCK_DATA id="6" first_name="Mariquilla" last_name="Caseri" email="mcaseri5@symantec.com" gender="Female" ip_address="247.187.208.116"/>
<MOCK_DATA id="7" first_name="Juline" last_name="Coase" email="jcoase6@indiatimes.com" gender="Female" ip_address="23.117.236.168"/>
<MOCK_DATA id="8" first_name="Courtnay" last_name="Scown" email="cscown7@ask.com" gender="Female" ip_address="120.80.78.72"/>
<MOCK_DATA id="9" first_name="Armin" last_name="Mc Caghan" email="amccaghan8@ameblo.jp" gender="Male" ip_address="244.47.41.88"/>
<MOCK_DATA id="10" first_name="Josie" last_name="Gunthorp" email="jgunthorp9@yahoo.co.jp" gender="Female" ip_address="169.142.65.100"/>
</dataset>
如何使用 javascript 或 JQuery 读取它并在 index.html 上显示它?提前致谢。
【问题讨论】:
【参考方案1】:您可以在 JavaScript 中使用 DomParser
来解析 XML。要获取标签内的属性,可以使用.getAttribute()
等方法
var xmlData = `<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<MOCK_DATA id="1" first_name="Sandor" last_name="Balmer" email="sbalmer0@qq.com" gender="Male" ip_address="148.134.84.45"/>
<MOCK_DATA id="2" first_name="Val" last_name="Toll" email="vtoll1@tamu.edu" gender="Male" ip_address="249.13.225.19"/>
<MOCK_DATA id="3" first_name="Phyllida" last_name="Purrier" email="ppurrier2@vimeo.com" gender="Female" ip_address="156.189.251.161"/>
<MOCK_DATA id="4" first_name="Marjy" last_name="Pittam" email="mpittam3@businessinsider.com" gender="Female" ip_address="193.195.178.210"/>
<MOCK_DATA id="5" first_name="Kirbee" last_name="Regglar" email="kregglar4@unblog.fr" gender="Female" ip_address="251.218.161.174"/>
<MOCK_DATA id="6" first_name="Mariquilla" last_name="Caseri" email="mcaseri5@symantec.com" gender="Female" ip_address="247.187.208.116"/>
<MOCK_DATA id="7" first_name="Juline" last_name="Coase" email="jcoase6@indiatimes.com" gender="Female" ip_address="23.117.236.168"/>
<MOCK_DATA id="8" first_name="Courtnay" last_name="Scown" email="cscown7@ask.com" gender="Female" ip_address="120.80.78.72"/>
<MOCK_DATA id="9" first_name="Armin" last_name="Mc Caghan" email="amccaghan8@ameblo.jp" gender="Male" ip_address="244.47.41.88"/>
<MOCK_DATA id="10" first_name="Josie" last_name="Gunthorp" email="jgunthorp9@yahoo.co.jp" gender="Female" ip_address="169.142.65.100"/>
</dataset>`
parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlData, "text/xml");
mock_data = xmlDoc.getElementsByTagName("MOCK_DATA");
for (let data in mock_data)
console.log(mock_data[data].getAttribute('first_name'))
【讨论】:
谢谢。但我必须安装 xmldom 才能使其工作。以上是关于如何使用 javascript 或 jquery 读取 DBUnit XML的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 JavaScript 或 jQuery 更改数组内对象的值?
如何使用 jquery 或 javascript 删除索引处的行? [复制]
如何使用 jQuery 或 JavaScript 设置底边距
如何使用 PHP 或 JavaScript/jQuery 禁用地址栏?