Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2006, 06:04 AM
Authorized User
 
Join Date: Nov 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Access specific Element and its Child elements!

How to retrieve a particular element and only its child elements from a xml? For example, if a user selects an employee, I want to map the selected employee and retrieve the data.

I am using XMLHttp object to load xml and I watn to access the elements using the method, getElementsByTagName("") in my javascript.

xml file:

<payslip>
<employee
     <name>SURESH</name>
     <id>E1020</id>
     <role>DEVELOPER</role>
</employee>
<employee
     <name>KUMAR</name>
     <id>E1030</id>
     <role>ANALYST</role>
</employee>
<employee
     <name>RAMESH</name>
     <id>E2050</id>
     <role>ACCOUNTANT</role>
</employee>
</payslip>

Javascript:

function showData(){
//Get the selected employee name from combobox.
var empName = comboBox.options[comboBox.selectedIndex].value;
//Assume that user has selected a name = KUMAR.

//Get the reference to <payslip> attribute.
var rootElement = xmlObj.getElementsByTagName('payslip');

//I am sure I am wrong with this for loop.
for (var i = 0; i < rootElement.length; i++) {

        if (getEmelementsByTagName('employee = KUMAR')) {
         //retrieve the child elements of particular <employee> element>
        }
 }

}


Thanks in advance!

Suresh.
__________________
Cheers [-:]

Suresh!
 
Old November 21st, 2006, 10:08 AM
Authorized User
 
Join Date: Nov 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Any suggestions!!!!!!

 
Old November 21st, 2006, 10:56 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
quote:Originally posted by suersh79
 Any suggestions!!!!!!

Yes, if you want a quick response pay for professional help.

You'd be better off using Xpath
Code:
xmlDoc.setProperty("SelectionLanguage", "XPath");
var empName = comboBox.options[comboBox.selectedIndex].value;
var sXPath = "/payslip/employee[name = '" + empName + "']";
var oEmployee = xmlObj.selectSingleNode(sXPath);
This assumes you're using Microsoft's XMLHttp, version 3.0 or greater.
If you have to use getElementsByTagName it's more difficult as you must select all employee elements:
Code:
var colEmployees = xmlObj.getElementsByTagName("employee");
Then loop through them all and test if .firstChild.text = empName.


--

Joe (Microsoft MVP - XML)
 
Old November 22nd, 2006, 12:58 AM
Authorized User
 
Join Date: Nov 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

quote:
--------------------------------------------------------------------------------
Originally posted by Joe (Microsoft MVP - XML)

Yes, if you want a quick response pay for professional help.
---------------------------------------------------------------------


I thought its a dicussion p2p Community Forum! Anyway if it has to be, how could that be done!
--------------------------------------------------------------

Nothing personal;).
Thanks Joe!
Does it works with Mozilla, Netscape?

Suresh.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Optional element exists if has inner elements 2BOrNot2B XML 0 May 9th, 2008 02:11 PM
Finding the first child element humansky XSLT 5 April 17th, 2008 08:46 AM
Checking for valid child elements. Chamkaur XSLT 0 January 25th, 2007 06:51 PM
child element kgoldvas XML 4 April 27th, 2006 01:51 AM
Multiple doc'ts w/ specific record elements counts kapy_kal XSLT 2 March 29th, 2006 11:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.