开发者

How to get subchild values from parent node from XML file parsing in JavaScript in XUL

开发者 https://www.devze.com 2023-03-16 07:37 出处:网络
How could I parse my email values to my text box according to email type.I need some help in parsing Emails values to my text-boxes.

How could I parse my email values to my text box according to email type. I need some help in parsing Emails values to my text-boxes.

<CONTACT>
<PDE-Identity>N65567</PDE-Identity>
<FirstName>Rangarajkarthik</FirstName>
<LastName>karthik Rangaraj</LastName>
<Facebook-ID/>
<EMAIL>
<Type>gmail</Type>
<Value>kart06@gmail.com</Value>
<Type>yahoo</Type>
<Value>karthikngaraj@yahoo.com</Value>
</EMAIL>
</CONTACT>

How can I parse the value of type to my textbox in XUL using a loop? In some contact nodes there is no Gmail and in some contact nodes there is no Yahoo email address. I'm really confused here...

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

This is what I have so far:

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

It's not working.

XUL code:

<?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].getElementsByT开发者_如何转开发agName("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>

My XML file:

   <?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>nura_ie@yahoo.co.in</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>mahmoud.ghbel@alcatel-lucent.com</Value>
</EMAIL>
</CONTACT>
<CONTACT>
<PDE-Identity>N65560</PDE-Identity>
<FirstName>keyankarthik</FirstName>
<LastName>karthik keyan</LastName>
<Facebook-ID/>
<EMAIL>
<Type>yahoo</Type>
<Value>karthye@yahoo.co.in</Value>
</EMAIL>
</CONTACT>
<CONTACT>
<PDE-Identity>N65567</PDE-Identity>
<FirstName>Rangarajkarthik</FirstName>
<LastName>karthik Rangaraj</LastName>
<Facebook-ID/>
<EMAIL>
<Type>gmail</Type>
<Value>kart06@gmail.com</Value>
<Type>yahoo</Type>
<Value>karthikngaraj@yahoo.com</Value>
</EMAIL>
</CONTACT>
<CONTACT>
<PDE-Identity>N65576</PDE-Identity>
<FirstName>ReddyAkky</FirstName>
<LastName>Akky Reddy</LastName>
<Facebook-ID/>
<EMAIL>
<Type>gmail</Type>
<Value>akkiredch@gmail.com</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>KURg_81@yahoo.com</Value>
</EMAIL>
</CONTACT>
</CONTACTS>


I think you are mixing up XUL and the XML format. XUL is a XML User Interface Language which mean that it describes the Use interface in an XML format.
The getElementsByTagName and getElementById functions aim at accessing XUL elements, i.e. UI elements, through their ids or tag name.
You cannot/shoudn't modify XML data using these functions. To process XML data in javascript you should use E4X. You can find a nice tutorial here. You should find the solution to what you need inside.

By the way, since the order of two siblings node in a XML data is not important, I would suggest you, for each Email address to create a parents node including the email value and the type:

<EMAILS>
  <EMAIL>
    <Type>gmail</Type>
    <Value>kart06@gmail.com</Value>
  </EMAIL>
  <EMAIL>    
    <Type>yahoo</Type>
    <Value>karthikngaraj@yahoo.com</Value>
  </EMAIL>
</EMAILS>
0

精彩评论

暂无评论...
验证码 换一张
取 消