473,796 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting only direct children in XML

This is only my first AJAX based application so this may be a trivial
question but I cannot find any way of getting at the data I want.

So, I have an XML file of the form:

<data>
<dataelement// one or more instances of this
Sep 21 '07 #1
4 2853
Andrew May wrote:
Using getElementsByTa gName on <datagets me an array containing all
<statuselemen ts including those of the child <dataelements>s . What I
need is some means to extract from <dataonly those <statuselemen ts
that are directly children of the <dataand not those that are children
of the child <dataelement> s. I could probably use LastChild to get what
I want but cannot guarantee that the <statuswill necessarily come
after the <dataelement> s.
With the core DOM you need to write a helper function that loops through
the childNodes and looks for the element nodes with the name you are
after (e.g. status).
Most browsers by now also support XPath so with Mozilla, Opera 9 you can
use the W3C DOM Level 3 XPath API to use an XPath expression of e.g.
status
relative to your data element.
With IE 6 and later there is also XPath support in the form of
selectSingleNod e/selectNodes exposed on DOM nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 21 '07 #2
Thanks to both of you. Looping through childNodes it is then. IE support
being a requirement and it not being a particularly big piece of XML.
Being new to this I wondered whether there might be some magic
incantation that I had missed but no real problem writing a bit of code.

Thanks again.

Andrew
Sep 21 '07 #3
On Sep 21, 4:17 am, Andrew May <andrew_d_...@h otmail.comwrote :
This is only my first AJAX based application so this may be a trivial
question but I cannot find any way of getting at the data I want.

So, I have an XML file of the form:

<data>
<dataelement// one or more instances of this
.
some data
.
<status>
.
some status information
.
</status>
</dataelement>
<status>
.
some status information
.
</status>
</data>

The significant feature is that both the enclosing <dataand each
<dataelementhav e <statuselemen ts that are identical in format so I
want to keep the <statustag.

I can use getElementsByTa gName to extract all the <dataelementsan d
then use getElementByTag Name to get all <statuselemen ts in each
<dataelement> . There should only be one so I can use FirstChild to
access it.

Using getElementsByTa gName on <datagets me an array containing all
<statuselemen ts including those of the child <dataelements>s . What I
need is some means to extract from <dataonly those <statuselemen ts
that are directly children of the <dataand not those that are children
of the child <dataelement> s. I could probably use LastChild to get what
I want but cannot guarantee that the <statuswill necessarily come
after the <dataelement> s.

I hope this is clear.

Thanks,

Andrew
This is actually a DOM question, which is an interface that's
implemented in lots of languages, so it's not strictly JS. Also,
XMLHttpRequest is unfortunately named - XML is quite heavyweight for
most Ajax applications; JSON is much easier to work with in
JavaScript, and takes much less code to traverse. Plus, it's easy for
server-side languages to parse; there are lots of free parsing
libraries out there for your language of choice, if you have the
misfortune to not be working in Python on the server ;) . If you
haven't yet, I'd recommend taking a look at JSON.

But to answer your question, you need node.childNodes . Be aware that,
as you iterate through this NodeList, you'll have to check the
NodeType property to ensure that it equals 1 (node.ELEMENT_N ODE),
because childNodes also may include text nodes, comment nodes, CDATA
nodes, etc., none of which have a tagName property. (Alternatively,
you may traverse the childNodes as a linked list, starting with
node.firstChild , then assigning successively node = node.nextSiblin g
until you reach node.lastChild) . Let me know if you need code samples
here. Good luck!

-David

Sep 21 '07 #4
David Golightly wrote:
This is actually a DOM question, which is an interface that's
implemented in lots of languages, so it's not strictly JS.
I did wonder about that - whether the question might be better in an XML
group. But, having said that I have had some useful answers from the
folks here.
XML is quite heavyweight for
most Ajax applications; JSON is much easier to work with in
JavaScript, and takes much less code to traverse. Plus, it's easy for
server-side languages to parse; there are lots of free parsing
libraries out there for your language of choice, if you have the
misfortune to not be working in Python on the server ;) . If you
haven't yet, I'd recommend taking a look at JSON.
That looks interesting. Don't think I've come across JSON before. I'll
take a look. What I am trying to achieve is not complex so JSON looks
ideal. I've plenty of programming experience but am new to all this
PHP/web/javascript stuff.

Thanks,

Andrew
Sep 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
17793
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
4
1474
by: Michael Hill | last post by:
If I had a span tag like: <span id='msg_2'> There is a bunch of text <a href="http://www.someurl.com"> here </a>. And some others <a href="http://www.otherurl.com">here</a>. Good luck! </span> And it contains other tags in it. If I use: document.getElementById('msg_2').firstChild.nodeValue
12
2021
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc.., which are suppose to describe the structure of my main TextFile class. Also i have created some custom collection classes, which only take items of the types, they are designed for. Now the problem is that I want my element classes, like...
1
1042
by: Sosh123 | last post by:
Hi, I'm sure there's a really easy way of doing this, but I haven't found it yet. I have a bunch of asp:hyperlinks. I have their IDs stored in a database. I want to be able to pull a record from the database and get a handle on the control with that ID. I thought FindControl might work something like this:
1
2029
by: Joseph Scoccimaro | last post by:
I am currently working on a project for school dealing with accessing the DOM. I am trying to get access to inner tables that are nested with in other tables. Currently I am able to get all parent tables without a problem. When I try to get an inner table through the child nodes it says that the tr elements of the parent table are undefined. Posted is the code I am using: function nestedTableAnalysis() {
0
1212
by: leenazsl | last post by:
I am currently working with a direct financial client in NJ/N/PA for staffing their long term project. The skills required are - C#, SQL Server, ADO.Net. I work as a Sr. Technical resource consultant for ZSL Inc. and at present actively hiring candidates as W2 and 1099 (new H1,H1 transfers) for various project opportunities with our end clients in the financial
3
27599
by: Andrej Pavlovic | last post by:
Hi, Please look at the following HTML snippet: <body> some text <p>more text</p> even more text </body>
8
2315
by: Jon Slaughter | last post by:
if I call javascript like <a href="javascript:;" onmousedown="toggle('a');">O</a> is there a way to pass the element a to the function without specifying any tags or names? I want the function to automatically return a "pointer" to the a link that it was called from instead of having to specify it.
5
7533
by: Sin Jeong-hun | last post by:
Hello. Speical folders, like Desktop, usually have different display names. For example, in Japanese Windows, it's displayed as "$B%G%9%/%H%C%W(B". I want to get the this displayed name of a special folder but I can't find a suitable method. Environment.GetFolderPath() is to get the path not the name. There's no way provided the .NET BCL? I also tried to get it through Win32 API. I looked up the MSDN site...
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.