2011-06-29 74 views
0

如何根據電子郵件類型將我的電子郵件值解析到我的文本框中。我需要一些幫助來解析電子郵件的值到我的文本框。如何從XUL中的JavaScript文件中的XML文件解析中獲得父節點的子值值

<CONTACT> 
<PDE-Identity>N65567</PDE-Identity> 
<FirstName>Rangarajkarthik</FirstName> 
<LastName>karthik Rangaraj</LastName> 
<Facebook-ID/> 
<EMAIL> 
<Type>gmail</Type> 
<Value>[email protected]</Value> 
<Type>yahoo</Type> 
<Value>[email protected]</Value> 
</EMAIL> 
</CONTACT> 

如何在XUL中使用循環解析類型值到我的文本框?在某些聯繫節點中沒有Gmail,並且在某些聯繫節點中沒有Yahoo電子郵件地址。我真的很困惑在這裏...

<row> 
    <label value="Gmail"/> 
    <textbox id="gmail" value=""/> 
</row> 
<row> 
    <label value="Yahoo"/> 
    <textbox id="yahoo" value=""/> 
</row> 

這是我到目前爲止有:

var str=x[i].getElementsByTagName("Value")[0].childNodes[0].nodeValue; 
document.getElementById("gmail").value=str; 

它不工作。

XUL代碼:

<?xml version="1.0"?> 
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> 
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" oncommand="init();"> 

    <grid> 
    <columns> 
     <column flex="1"/> 
     <column flex="4"/> 
     <column flex="4"/> 
     <column flex="4"/> 
     <column flex="4"/> 
    </columns> 

    <rows> 
     <row> 
<label control="" value="Select PDE-ID:"/> 
<menulist id="List" oncomman=""> 
       <menupopup id="Popup"> 
       </menupopup> 
</menulist> 

     </row> 
    </rows> 
</grid> 

<label control="" accesskey="" value=""/> 
    <grid> 
    <columns> 
     <column flex="1"/> 
     <column flex="4"/> 
     <column flex="4"/> 
     <column flex="4"/> 
<column flex="4"/> 
    </columns> 

    <rows> 
     <row> 
     <label value="PDE-Identity"/> 
     <textbox id="pde" value=""/> 
     </row> 
     <row> 
     <label value="FirstName"/> 
     <textbox id="fname" value=""/> 
     </row> 
     <row> 
     <label value="LastName"/> 
     <textbox id="lname" value=""/> 
     </row> 
     <row> 
      <label value="Gmail"/> 
     <textbox id="gmail" value=""/> 
     </row> 
     <row> 
      <label value="Yahoo"/> 
     <textbox id="yahoo" value=""/> 
     </row> 
     <row> 
      <label value="Alcatel-Lucent"/> 
     <textbox id="alcatel-lucent" value="" /> 
     </row> 
     <row> 
      <label value="URL"/> 
     <textbox id="facebook" value="" /> 
     </row> 
     <row> 
      <label value="Facebook-ID"/> 
     <textbox id="fid" value="" /> 
     </row> 
    </rows> 
    </grid> 
<label value=""/> 
<grid> 
    <columns> 
     <column flex=""/> 
    <hbox align="center" > 
     <button id="save" label="next" oncommand="next()" /> 
     <button id="cancel" label="previous" oncommand="previous()"/> 
     <button id="delete" label="Merge" oncommand="delete();"/>   
    </hbox> 
</columns> 
</grid> 


<separator/> 
<text value="© 2011"/> 
<separator/> 


<script type="application/x-javascript"> 
<![CDATA[ 




var objXMLDoc = document.implementation.createDocument('','doc',null); 
var objXMLHTTP = new XMLHttpRequest(); 

objXMLHTTP.open("GET", "file://D:/xmlparserinxul/final.xml", false); 
objXMLHTTP.send(null); 
objXMLDoc = objXMLHTTP.responseXML; 

var objNodeList = objXMLDoc.getElementsByTagName("PDE-Identity"); 
    for (var i=0; i<objNodeList.length; i++){ 
      var menuPopup=document.getElementById('Popup');    
        var menu1=document.createElement("menuitem"); 
         menu1.setAttribute("label",'objNodeList'); 
         menuPopup.appendChild(menu1); 
} 


var x = objXMLDoc.getElementsByTagName("CONTACT"); 
i=0; 

function init() 
{ 

var str=x[i].getElementsByTagName("PDE-Identity")[0].childNodes[0].nodeValue; 
document.getElementById("pde").value=str; 

var str=x[i].getElementsByTagName("FirstName")[0].childNodes[0].nodeValue; 
document.getElementById("fname").value=str; 

var str=x[i].getElementsByTagName("LastName")[0].childNodes[0].nodeValue; 
document.getElementById("lname").value=str; 

/*var str=x[i].getElementsByTagName("Type")[0].childNodes[0].nodeValue; 
document.getElementById("gmail").value=str; 

var str=x[i].getElementsByTagName("Type")[0].childNodes[0].nodeValue; 
document.getElementById("yahoo").value=str; 

var str=x[i].getElementsByTagName("Type")[0].childNodes[0].nodeValue; 
document.getElementById("alcatel-lucent").value=str; */ 

var str=x[i].getElementsByTagName("URL")[0].childNodes[0].nodeValue; 
document.getElementById("facebook").value=str; 

var str=x[i].getElementsByTagName("Facebook-ID")[0].childNodes[0].nodeValue; 
document.getElementById("fid").value=str; 

} 

function next() 
{ 

document.getElementById('facebook').value=''; 
document.getElementById('fid').value=''; 
//alert('fool'); 
if (i<x.length-1) 
    { 
    i++; 
    init(); 

    } 
} 

function previous() 
{ 

document.getElementById('facebook').value=''; 
document.getElementById('fid').value=''; 

if (i>0) 
    { 

    i--; 
    init(); 

    } 
} 


]]> 
</script> 

</window> 

我的XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<CONTACTS> 
<CONTACT> 
<PDE-Identity>N65539</PDE-Identity> 
<FirstName>Arun_niit</FirstName> 
<LastName>Arun_niit</LastName> 
<Facebook-ID/> 
<EMAIL> 
<Type>yahoo</Type> 
<Value>[email protected]</Value> 
</EMAIL> 
</CONTACT> 
<CONTACT> 
<PDE-Identity>N65546</PDE-Identity> 
<FirstName>FodenBen'</FirstName> 
<LastName>Ben' Foden</LastName> 
<URL>http://www.facebook.com/profile.php?id=100002440474277</URL> 
<Facebook-ID>100002440474277</Facebook-ID> 
<EMAIL/> 
</CONTACT> 
<CONTACT> 
<PDE-Identity>N65553</PDE-Identity> 
<FirstName>GhorbelMahmoud</FirstName> 
<LastName>Mahmoud Ghorbel</LastName> 
<Facebook-ID/> 
<EMAIL> 
<Type>alcatel-lucent</Type> 
<Value>[email protected]</Value> 
</EMAIL> 
</CONTACT> 
<CONTACT> 
<PDE-Identity>N65560</PDE-Identity> 
<FirstName>keyankarthik</FirstName> 
<LastName>karthik keyan</LastName> 
<Facebook-ID/> 
<EMAIL> 
<Type>yahoo</Type> 
<Value>[email protected]</Value> 
</EMAIL> 
</CONTACT> 
<CONTACT> 
<PDE-Identity>N65567</PDE-Identity> 
<FirstName>Rangarajkarthik</FirstName> 
<LastName>karthik Rangaraj</LastName> 
<Facebook-ID/> 
<EMAIL> 
<Type>gmail</Type> 
<Value>[email protected]</Value> 
<Type>yahoo</Type> 
<Value>[email protected]</Value> 
</EMAIL> 
</CONTACT> 
<CONTACT> 
<PDE-Identity>N65576</PDE-Identity> 
<FirstName>ReddyAkky</FirstName> 
<LastName>Akky Reddy</LastName> 
<Facebook-ID/> 
<EMAIL> 
<Type>gmail</Type> 
<Value>[email protected]</Value> 
</EMAIL> 
</CONTACT> 
<CONTACT> 
<PDE-Identity>N65583</PDE-Identity> 
<FirstName>SandfordFrankie</FirstName> 
<LastName>Frankie Sandford</LastName> 
<URL>http://www.facebook.com/FrankieSandfordApprovedPage</URL> 
<Facebook-ID/> 
<EMAIL/> 
</CONTACT> 
<CONTACT> 
<PDE-Identity>N65590</PDE-Identity> 
<FirstName>TheSatsRochelle</FirstName> 
<LastName>Rochelle TheSats</LastName> 
<URL>http://www.facebook.com/profile.php?id=100002487211054</URL> 
<Facebook-ID>100002487211054</Facebook-ID> 
<EMAIL/> 
</CONTACT> 
<CONTACT> 
<PDE-Identity>N65597</PDE-Identity> 
<FirstName>KumarVeera</FirstName> 
<LastName>Vea_Kumar</LastName> 
<Facebook-ID/> 
<EMAIL> 
<Type>yahoo</Type> 
<Value>[email protected]</Value> 
</EMAIL> 
</CONTACT> 
</CONTACTS> 
+1

只是一個小小的評論:也許你不應該發佈你的XML文件的真實內容,我不會很高興,所以找到我自己的郵件地址張貼在網上... – revers

+0

電子郵件地址被改變。我希望有一個人可以幫助我.... – user838691

回答

1

我想你混淆了XUL和XML格式。 XUL是XML用戶界面語言,這意味着它描述了XML格式的Use界面。
getElementsByTagName和getElementById函數旨在通過它們的ID或標記名稱來訪問XUL元素,即UI元素。
使用這些函數不能/不應該修改XML數據。要在JavaScript中處理XML數據,您應該使用E4X。你可以找到一個不錯的教程here。你應該找到你需要的解決方案。

順便說一句,因爲兩個兄妹節點在XML數據的順序並不重要,我建議你爲每個電子郵件地址創建包括電子郵件值和類型的父母節點:

<EMAILS> 
    <EMAIL> 
    <Type>gmail</Type> 
    <Value>[email protected]</Value> 
    </EMAIL> 
    <EMAIL>  
    <Type>yahoo</Type> 
    <Value>[email protected]</Value> 
    </EMAIL> 
</EMAILS> 
相關問題