469,267 Members | 1,094 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

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
attribute?

Whether my XML file is well formed or not?

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

Thanks in advance.
Robert

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

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

<Window WinID="2">
<Views>
<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>
</Views>
</Window>
</WindowsInfo>
<ObjectInfo>
<Object ObjectID="1">
<ObjectFields>
<Field FieldID="1">
<Title>Sequence</Title>
<DataType>2</DataType>
</Field>
<Field FieldID="2">
<Title>Value 1</Title>
<DataType>0</DataType>
</Field>
<Field FieldID="3">
<Title>Value 2</Title>
<DataType>1</DataType>
</Field>
<Field FieldID="4">
<Title>Value 3</Title>
<DataType>2</DataType>
</Field>
<Field FieldID="5">
<Title>Value 4</Title>
<DataType>3</DataType>
</Field>
<Field FieldID="6">
<Title>Value 5</Title>
<DataType>4</DataType>
</Field>
<Field FieldID="7">
<Title>Value 6</Title>
<DataType>2</DataType>
</Field>
<Field FieldID="8">
<Title>Value 7</Title>
<DataType>5</DataType>
</Field>
</ObjectFields>
</Object>
<Object ObjectID="2">
<ObjectFields>
<Field FieldID="1">
<Title>Sequence</Title>
<DataType>2</DataType>
</Field>
<Field FieldID="2">
<Title>Value 1</Title>
<DataType>0</DataType>
</Field>
<Field FieldID="3">
<Title>Value 2</Title>
<DataType>0</DataType>
</Field>
</ObjectFields>
</Object>
</ObjectInfo>
</Configuration>

Nov 12 '05 #1
2 4989


Robert wrote:

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


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
http://JavaScript.FAQTs.com/
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
expression.

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
XML.

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.

Cheers,
Stuart Celarier, Fern Creek

[1]
http://msdn.microsoft.com/library/en...odestopic1.asp
[2] http://www.w3.org/TR/1999/REC-xpath-19991116
[3]
http://msdn.microsoft.com/library/en...tbyidtopic.asp
[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
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.