471,594 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Random access of XML file

I have the XML file which has a structure similar to one I pasted at the end
of this post. As you can see many elements are using different id attributes.
For example element Window uses WinID attribute as ID, the ViewData uses
ViewID attribute as an ID and so on. I need to read this file without
assuming what is the order of the elements, in other words I can't read it
sequentially. The reason for this requirement is that whole WindowsInfo
element might be missing for example or Object Info might be first and
WindowsInfo second etc.

Since many elements have the same name and differ only by their id attribute
I can't use XmlDocument::GetElementsByTagName method.

Of course I can declare id attributes through DTD and use
XmlDocument::GetElementById, but this approach won't work either. The
GetElementById method returns first element with the specified id, which
means call to GetElementById("3") will always return first ViewData element
which is of course wrong in my case.

I wonder whether there are other methods for random access XML file by id

Whether my XML file is well formed or not?

Can anyone suggest better way of accessing data in my case?

Thanks in advance.

************************************************** ****

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Window WinID="1">
<ViewData ViewID="1">somedata1</ViewData>
<ViewData ViewID="2">somedata2</ViewData>
<ViewData ViewID="3">somedata3</ViewData>

<Window WinID="2">
<ViewData ViewID="1">somedata21</ViewData>
<ViewData ViewID="2">somedata22</ViewData>
<ViewData ViewID="3">somedata23</ViewData>
<ViewData ViewID="4">somedata24</ViewData>
<ViewData ViewID="5">somedata25</ViewData>
<ViewData ViewID="6">somedata26</ViewData>
<Object ObjectID="1">
<Field FieldID="1">
<Field FieldID="2">
<Title>Value 1</Title>
<Field FieldID="3">
<Title>Value 2</Title>
<Field FieldID="4">
<Title>Value 3</Title>
<Field FieldID="5">
<Title>Value 4</Title>
<Field FieldID="6">
<Title>Value 5</Title>
<Field FieldID="7">
<Title>Value 6</Title>
<Field FieldID="8">
<Title>Value 7</Title>
<Object ObjectID="2">
<Field FieldID="1">
<Field FieldID="2">
<Title>Value 1</Title>
<Field FieldID="3">
<Title>Value 2</Title>

Nov 12 '05 #1
2 5050

Robert wrote:

I wonder whether there are other methods for random access XML file by id

Do you know XPath? There you could access
xmlDocument.SelectNodes("//*[contains(local-name(@*), 'ID')]")
that yields all elements at every level which have an attribute where
the attribute name contains 'ID'.
If you wanted to access the first element that has the somethingID
attribute value 3 you could use
xmlDocument.SelectSingleNode("//*[@*[contains(local-name(), 'ID') and
.. = 3]]")

Martin Honnen
Nov 12 '05 #2
As Martin suggested, you can use the XmlNode.SelectNodes method [1] to
select all nodes matching an XPath expression [2]. XmlNode.SelectSingleNode
selects the first node (in document order) that matches the XPath

The XmlDocument.GetElementByID method requires use of a DTD to identify
which attributes are treated as IDs [3]. DTDs are very nearly dead, and you
are better served in the long term by not relying on them.

If your document is not well-formed XML, then all bets are off.
Fundamentally, if it's not well-formed XML, then it isn't XML, period. In
the case of XmlDocument, you can't load a document that is not well-formed

If you want to go a step further beyond XPath support (SelectNodes), you can
look at XSLT [4], but that may be overkill depending on your needs. The XSLT
element <xsl:key> acts like an index over the XML source document, and the
key() function acts like a SELECT statement over that index. This has the
potential of delivering better performance.

Here is a key that indexes all elements having at least one attribute whose
name contains the string 'ID', indexed by the values of any such attributes.

<xsl:key name="AllIDs"
match="*[contains(local-name(@*), 'ID')]"
use="@*[contains(local-name(.), 'ID')]"/>

That key can be used in expressions in XSLT elements like this

key('AllIDs', '3')

This selects all elements anywhere in the document that have at least one
attribute whose name contains the string 'ID' and whose value is 3. (Caveat,
I'm just writing this XSLT off the top of my head to give you the flavor.)

I should point out that XSLT is a general purpose transform language for XML
and can take a while to learn. It may be much more than you need or are
willing to undertake.

Stuart Celarier, Fern Creek

[2] http://www.w3.org/TR/1999/REC-xpath-19991116
[4] http://www.w3.org/TR/1999/REC-xslt-19991116
Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Oin Zea | last post: by
3 posts views Thread by Rob B | last post: by
reply views Thread by jeff | last post: by
1 post views Thread by rayw | last post: by
6 posts views Thread by comp.lang.php | last post: by
16 posts views Thread by Claudio Grondi | last post: by
39 posts views Thread by Alan Isaac | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.