2017-05-04 91 views
0
<?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需要幫助讀取XML @ JS

如果你能幫助我將很樂意

+1

你嘗試過這麼遠嗎? – jediz

+0

var document = XMLManager.fromString(myString); var members = document.getElementsByTagName(「vmext:EntityLink」)。getAttribute(「id」); SYSTEM.LOG(members.length) 爲(I = 0; I

+0

我得到這樣的過時間 「無法調用 」的Java腳本它在側面的VMware vrealize配器 VAR文件= XMLManager.fromString(MyString的)「 空的」 –

回答

0

它的一個小錯字。在下面的行中將您的ID替換爲id。

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> 
+0

getAttributeNode; var members = document.getElementsByTagName(「vmext:EntityLink」); SYSTEM.LOG(members.length) 爲(I = 0; I

+0

讀取屬性「attributes」以查找元素移除名稱空間「vmtext:」 – Yoganand