<?xml version="1.0" encoding="UTF-8"?>
<vmext:Notification xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5" eventId="cd35c994-5e3b-4947-a435-bae3c8886af5" type="com/vmware/vcloud/event/vm/create" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/extension/v1.5 http://vcd.hostcenter.dk/api/v1.5/schema/vmwextensions.xsd">
<vmext:EntityLink rel="entity" id="urn:vcloud:vm:f76c9a4c-b94f-4019-a014-359c8107bc18" name="Windows Server 2016 Datacenter-1" type="vcloud:vm"/>
<vmext:EntityLink rel="down" id="urn:vcloud:user:3c35fb93-39a2-4fc6-aad7-5d06e138e9e4" name="system" type="vcloud:user"/>
<vmext:EntityLink rel="up" id="urn:vcloud:org:bb878c05-b433-469f-8d76-5f17b89836de" name="xxxxx.xxx" type="vcloud:org"/>
<vmext:Timestamp>2017-05-03T17:04:46.311+02:00</vmext:Timestamp>
<vmext:OperationSuccess>true</vmext:OperationSuccess>
</vmext:Notification>
System.log(myString) <-- the xml data from top
var document = XMLManager.fromString(myString);
var members = document.getElementsByTagName("vmext:EntityLink");
System.log(members.length)
for (i = 0; i < members.length; i++){
System.log(members[i].attributes["ID"].value)
}
我想从vmext获取所有id:EntityLink我只返回NULL
如果你能帮助我会很开心
答案 0 :(得分:0)
System.log(members[i].attributes["ID"].value)
并且您不需要在查找标记或元素时包含名称空间。所以从下一行中删除vmext:
var members = document.getElementsByTagName("vmext:EntityLink");
这是工作代码,
<!DOCTYPE html>
<html>
<head>
<style>
.democlass {
color: red;
}
</style>
</head>
<body>
<h1 class="democlass">Hello World</h1>
<p>Click the button to display the value of the class attribute of the h1 element.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var xml = '<?xml version="1.0" encoding="UTF-8"?>'
+'<vmext:Notification xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5" eventId="cd35c994-5e3b-4947-a435-bae3c8886af5" type="com/vmware/vcloud/event/vm/create" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/extension/v1.5 http://vcd.hostcenter.dk/api/v1.5/schema/vmwextensions.xsd">'
+'<vmext:EntityLink rel="entity" id="urn:vcloud:vm:f76c9a4c-b94f-4019-a014-359c8107bc18" name="Windows Server 2016 Datacenter-1" type="vcloud:vm"/>'
+'<vmext:EntityLink rel="down" id="urn:vcloud:user:3c35fb93-39a2-4fc6-aad7-5d06e138e9e4" name="system" type="vcloud:user"/>'
+'<vmext:EntityLink rel="up" id="urn:vcloud:org:bb878c05-b433-469f-8d76-5f17b89836de" name="xxxxx.xxx" type="vcloud:org"/>'
+'<vmext:Timestamp>2017-05-03T17:04:46.311+02:00</vmext:Timestamp>'
+'<vmext:OperationSuccess>true</vmext:OperationSuccess>'
+'</vmext:Notification>'
var document = parseXML(xml)
var members =
document.getElementsByTagName("vmext:EntityLink");
console.log(members.length)
for (i = 0; i < members.length; i++){
console.log(members[i].attributes["ID"].value)
}
}
function parseXML(val) {
if (document.implementation &&
document.implementation.createDocument) {
xmlDoc = new DOMParser().parseFromString(val,
'text/xml');
}
else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.loadXML(val);
}
else
{
alert("Your browser can't handle this script");
return null;
}
return xmlDoc;
}
</script>
</body>
</html>