473,581 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::Ge tElementsByTagN ame method.

Of course I can declare id attributes through DTD and use
XmlDocument::Ge tElementById, 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 "?>
<Configuratio n>
<Window WinID="1">
<ViewData ViewID="1">some data1</ViewData>
<ViewData ViewID="2">some data2</ViewData>
<ViewData ViewID="3">some data3</ViewData>

<Window WinID="2">
<ViewData ViewID="1">some data21</ViewData>
<ViewData ViewID="2">some data22</ViewData>
<ViewData ViewID="3">some data23</ViewData>
<ViewData ViewID="4">some data24</ViewData>
<ViewData ViewID="5">some data25</ViewData>
<ViewData ViewID="6">some data26</ViewData>
<Object ObjectID="1">
<ObjectFields >
<Field FieldID="1">
<Title>Sequence </Title>
<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">
<ObjectFields >
<Field FieldID="1">
<Title>Sequence </Title>
<Field FieldID="2">
<Title>Value 1</Title>
<Field FieldID="3">
<Title>Value 2</Title>

Nov 12 '05 #1
2 5161

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.Sel ectNodes("//*[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.Sel ectSingleNode("//*[@*[contains(local-name(), 'ID') and
.. = 3]]")

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

The XmlDocument.Get ElementByID 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Oin Zea | last post by:
Is it possible for to program to access a random file at the same time and perform actions like create a new record?
by: Rob B | last post by:
Hello, I am just starting to learn Python and was writing a simple script on my machine (Mac OS X 10.3.4), but I can't seem to import the random module: #!/usr/bin/env python import random
by: jeff | last post by:
i'm using "visual basic.net standard ver 2003". i'm running windows xp with sp2. i'm trying to make a random access file. it will make the file. but the file will have no data in it. my program is listed below. Structure jeff Public id As Integer Public g1 As Short Public g2 As Short Public g3 As Short Public g4 As Short 'number of games
by: rayw | last post by:
I was wondering if there were some good on-line references to the various pros/cons with these types of file access at all? Ta rayw
by: VB.NET | last post by:
I'm using a mysql database and connecting my vb.net program to the DB over a network connection. i would like to bring this data over to a vb.net random access file. does anyone know how to convert my DB into a binary file? and how to convert my code to use a file instead of the db?
by: Bruce | last post by:
I am building a WinForms app that uses Web Services access to a server for most of its data input/output, but I also need to persist some of its data to the local disk (basically as a cache of some of the Web Services data) in XML format. Since the size of the XML local store could be rather large, I'd prefer to have a random access...
by: comp.lang.php | last post by:
/** * Generate the random security image * * @access public * @param $willUseFilePath (default false) boolean to determine if you will be using a file path * @param mixed $filePath (optional) file path to store image resource object contents * @see actual_path */
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain unchanged). How can I do that in Python? Claudio Grondi
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and MyClass from module2. module2 does not import random. module1 sets a seed like this::
by: knuckels23 | last post by:
Hi All, I have a Random access file which is written using VB 6.0. I need to read this file using C#. The record used in VB to write the Random access file is as follows Type AA aa1 As Integer aa2 As Integer aa3 As Integer aa4 As Integer
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...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
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...
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...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
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...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.