473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

need to parse XML file without root elem

Hello,
I need to parse this XML file or XML stream . Is it possible to parse
it with DOM reading each DataContainer one by one.

<DataContainer>
<name> </name>
<birthDate> </birthDate>
<personnalInfos>
<personnalInfo_entries> </personnalnfo_entries>
<personnalInfo_elem> </personnalnfo_elem>
<personnalInfo_elem> </personnalnfo_elem>
</personnalInfos>
</DataContainer>
<DataContainer>
<name> </name>
<birthDate> </birthDate>
<personnalInfos>
<personnalInfo_entries> </personnalnfo_entries>
<personnalInfo_elem> </personnalnfo_elem>
<personnalInfo_elem> </personnalnfo_elem>
</personnalInfos>
</DataContainer>

Cheers.

Apr 18 '06 #1
7 3695
> I need to parse this XML file or XML stream . Is it possible to parse
it with DOM reading each DataContainer one by one.
No, DOM documents are required to have exactly one root node, not several.
<DataContainer>
<name> </name>
<birthDate> </birthDate>
<personnalInfos>
<personnalInfo_entries> </personnalnfo_entries>
<personnalInfo_elem> </personnalnfo_elem>
<personnalInfo_elem> </personnalnfo_elem>
</personnalInfos>
</DataContainer>
<DataContainer>
<name> </name>
<birthDate> </birthDate>
<personnalInfos>
<personnalInfo_entries> </personnalnfo_entries>
<personnalInfo_elem> </personnalnfo_elem>
<personnalInfo_elem> </personnalnfo_elem>
</personnalInfos>
</DataContainer>

Apr 18 '06 #2
What is the solution then? I can not have a root elem solution as I can
have a hudge file.

parse with sax? Any idea?
Thx

Apr 18 '06 #3
In article <11**********************@j33g2000cwa.googlegroups .com>,
akira <ak*******@yahoo.fr> wrote:
What is the solution then? I can not have a root elem solution as I can
have a hudge file.


You won't be able to read it with an XML parser if you don't put a
root element around it. I'm not sure why the size of the file should
make it harder to add one.

One way to get a root element is to create a short file with an entity
reference pointing to the "real" data.

<!DOCTYPE dummy [
<!ENTITY data SYSTEM "http://wherever-my-data-is">
]>
<dummy>
&data;
</dummy>

--- Richard
Apr 18 '06 #4
akira wrote:
What is the solution then? I can not have a root elem solution as I can
have a hudge file.

parse with sax? Any idea?


Afaik, SAX is more suitable for large documents than DOM. I don't know if it
supports multiple root nodes. With SAX, afaik, it's not possible to modify
or create documents, you can only parse them and then they are static.
You might also have a look at JDOM or DOM4J, though I don't know much about
these. Just to give you some keywords.

I don't yet understand why having one root node instead of several is a
problem with large documents.

If the reason is that you want to split your data across multiple files, you
could still have a single root node in each one, and then use DOM to
assemble them after you have loaded them. Or (which is a bit dirty) you just
concatenate the files as strings, write "<my-root-node>" at the beginning
and "</my-root-node>" at the end and then parse this string using DOM.

Hope this helps.

Regards,

Felix
Apr 18 '06 #5
Felix Engelhardt wrote:
I need to parse this XML file or XML stream . Is it possible to parse
it with DOM reading each DataContainer one by one.


No, DOM documents are required to have exactly one root node, not several.


That's true. Especially because well-formed XML files
are allowed to have one root only.
<DataContainer>
<name> </name>
<birthDate> </birthDate>
<personnalInfos>
<personnalInfo_entries> </personnalnfo_entries>
<personnalInfo_elem> </personnalnfo_elem>
<personnalInfo_elem> </personnalnfo_elem>
</personnalInfos>
</DataContainer>
<DataContainer>
<name> </name>
<birthDate> </birthDate>
<personnalInfos>
<personnalInfo_entries> </personnalnfo_entries>
<personnalInfo_elem> </personnalnfo_elem>
<personnalInfo_elem> </personnalnfo_elem>
</personnalInfos>
</DataContainer>


The subject of parsing XML data with multiple roots
comes up about once each month. If you are bound to
usual XSL processors or DOM libraries, then there's
no way out for you. If you are free to choose a tool
with a SAX-like processing style, then you might be
lucky. For example, the XMLgawk extension of the GNU
Awk interpreter allows you to parse XML data with
multiple roots:

http://home.vrweb.de/~juergen.kahrs/...wk-interpreter

One of the authors (Andrew Schorr) processes GigaBytes
of such data and stores it in a database.
Apr 18 '06 #6
Thanks a lot

Apr 18 '06 #7
Felix Engelhardt wrote:
No, DOM documents are required to have exactly one root node, not several.


Because a well-formed XML document is required to have exactly one
top-level element, not several. The file you've shown us is not an XML
document, though it may be an XML External Entity.

DOM DocumentFragments can have several top-level elements. But I don't
know of an off-the-shelf parser that will read an External Entity
directly into a DocumentFragment.

One solution is to set up a bit of additional code which wraps a
top-level element around the data as it's read in -- for example, set up
a SAXFilter which generates an extra startElement after the
startDocument event and an extra endElement before the endDocument
event. That does require that the rest of your code be aware this dummy
element was introduced, but that's usually straightforward... or you can
move these trees into a DocumentFragment, if you're working with DOMs.

Saving the data back out may be complicated since, like parsers,
serializers may or may not assume they're intended to process
well-formed XML document. Generally, though, they're more forgiving and
you can get away with using the standard serializer; it's just your
responsibility not to write out the dummy wrapper element.

Good luck.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Apr 18 '06 #8

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

Similar topics

3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
3
by: Sarav | last post by:
Hi All, I need to upload an XML file via an client side ActiveX control. I searched the web but everywhere got the samples of using file control alone. How can I upload a file into my web server...
3
by: kim | last post by:
GOOD morning, I try to open xx.vbs in <href="./xx.vbs"></hef> but it pop up the file download mesage. How can i disabled it? I try another solution like this : set ws =...
4
by: doritrieur | last post by:
How do i read line from an input file, without the /n ? the readline function returns also the /n character at the end. i need to read a line without the training /n is this possible? thanks,...
15
by: MCondon77 | last post by:
This is probably a common problem, but as a novice programmer I'm trying to figure out how to write 0x0A to a BIN file without having it automatically write 0x0D. Here's an example of the code:...
18
by: John Bailo | last post by:
I want to write an Excel file (.xls format) from some database data. I don't want to use Excel.exe because of all the automation and security issues. Does Microsoft document the .xls file...
2
by: solargovind | last post by:
Hi, Can anybody suggest me how to take data from an Excel file without import into Access as a Table..? Bcoz...I hve several file. if i import, it would take more time and increase the file...
2
by: vishalgupta | last post by:
i need to read/write to a text/binary file without using any header file in c. i cant use the FILE strucutire as it is defined in stdio.h nor can i use file input/output functions as they are...
11
by: MC | last post by:
I have a couple of customers who are using a program that deserializes an XML file, and on some files, the program fails to see the content of the file -- treats it as zero length and reports "root...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...
0
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...

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.