Basic XML question
Discussion
Can you have more than 1 Root element in a File.
I always been under the impression 1 Root element per file only, howeverI have just got some data which doesn't match out XML tree,
First example
<Root>
<MessageType>all the repeating stuff in here</MessageType>
<MessageType>all the repeating stuff in here</MessageType>
<MessageType>all the repeating stuff in here</MessageType>
</Root>
Second example
the Data I have just received
<Root>all the repeating stuff in here</Root>
<Root>all the repeating stuff in here</Root>
<Root>all the repeating stuff in here</Root>
My question is,
Is it valid to receive a file that contains a reapeating Root element ?
I always been under the impression 1 Root element per file only, howeverI have just got some data which doesn't match out XML tree,
First example
<Root>
<MessageType>all the repeating stuff in here</MessageType>
<MessageType>all the repeating stuff in here</MessageType>
<MessageType>all the repeating stuff in here</MessageType>
</Root>
Second example
the Data I have just received
<Root>all the repeating stuff in here</Root>
<Root>all the repeating stuff in here</Root>
<Root>all the repeating stuff in here</Root>
My question is,
Is it valid to receive a file that contains a reapeating Root element ?
No you can't.
Its a bit of a pain, and prevents doing things like log files in XML since you can't just append new elements.
You can always leave a header root element open, and append subsequent elements as child elements, and finally close the root element when you close the file, but this leaves the XML inconsistent until you finally close it.
Its a bit of a pain, and prevents doing things like log files in XML since you can't just append new elements.
You can always leave a header root element open, and append subsequent elements as child elements, and finally close the root element when you close the file, but this leaves the XML inconsistent until you finally close it.
"One file" => "one root" is not the same as saying "One XML document" => "one root". The latter is true, but surely the former isn't? Why can't one file can contain several consecutive XML documents? (Also don't see the problem with log files, except for the verbosity being even worse than it usually is with XML.)
Edited by ATG on Friday 2nd March 11:05
ATG said:
(Also don't see the problem with log files, except for the verbosity being even worse than it usually is with XML.)
SNE explained that one. While you can append records after the opening root element the xml file won't be well formed until you close the root element. If I needed an xml log file I'd be tempted to write a service that accepted logging messages and kept them in memory and wrote a well formed xml document from that on some event. toni896 said:
Can you have more than 1 Root element in a File.
I always been under the impression 1 Root element per file only, howeverI have just got some data which doesn't match out XML tree,
First example
<Root>
<MessageType>all the repeating stuff in here</MessageType>
<MessageType>all the repeating stuff in here</MessageType>
<MessageType>all the repeating stuff in here</MessageType>
</Root>
Second example
the Data I have just received
<Root>all the repeating stuff in here</Root>
<Root>all the repeating stuff in here</Root>
<Root>all the repeating stuff in here</Root>
My question is,
Is it valid to receive a file that contains a reapeating Root element ?
I always been under the impression 1 Root element per file only, howeverI have just got some data which doesn't match out XML tree,
First example
<Root>
<MessageType>all the repeating stuff in here</MessageType>
<MessageType>all the repeating stuff in here</MessageType>
<MessageType>all the repeating stuff in here</MessageType>
</Root>
Second example
the Data I have just received
<Root>all the repeating stuff in here</Root>
<Root>all the repeating stuff in here</Root>
<Root>all the repeating stuff in here</Root>
My question is,
Is it valid to receive a file that contains a reapeating Root element ?
You can, but you'd need to load it into an XMLNodeList rather than an XMLDocument in your code if you need to parse it.
dern said:
ATG said:
(Also don't see the problem with log files, except for the verbosity being even worse than it usually is with XML.)
SNE explained that one. While you can append records after the opening root element the xml file won't be well formed until you close the root element. If I needed an xml log file I'd be tempted to write a service that accepted logging messages and kept them in memory and wrote a well formed xml document from that on some event. You've missed the point. The log file can be a set of well formed XML documents not one XML docuemnt that is left open. I don't see the problem with one file containing more than one XML document. Each time you log to the log file it will add a complete XML document to the file, not a single new element.
ATG said:
dern said:
ATG said:
(Also don't see the problem with log files, except for the verbosity being even worse than it usually is with XML.)
SNE explained that one. While you can append records after the opening root element the xml file won't be well formed until you close the root element. If I needed an xml log file I'd be tempted to write a service that accepted logging messages and kept them in memory and wrote a well formed xml document from that on some event. You've missed the point. The log file can be a set of well formed XML documents not one XML docuemnt that is left open. I don't see the problem with one file containing more than one XML document. Each time you log to the log file it will add a complete XML document to the file, not a single new element.
dern said:
ATG said:
dern said:
ATG said:
(Also don't see the problem with log files, except for the verbosity being even worse than it usually is with XML.)
SNE explained that one. While you can append records after the opening root element the xml file won't be well formed until you close the root element. If I needed an xml log file I'd be tempted to write a service that accepted logging messages and kept them in memory and wrote a well formed xml document from that on some event. You've missed the point. The log file can be a set of well formed XML documents not one XML docuemnt that is left open. I don't see the problem with one file containing more than one XML document. Each time you log to the log file it will add a complete XML document to the file, not a single new element.
What other point could I possibly have been making? XML is always spectaculary inefficient in terms of the size of the document compared to the data it contains. That is a given.
ATG said:
What other point could I possibly have been making? XML is always spectaculary inefficient in terms of the size of the document compared to the data it contains. That is a given.
In some cases the benefits of the inherent structure outweighs the verbosity of the format but in your example there is no structure so no advantage. That's why I didn't think you were serious. dern said:
ATG said:
What other point could I possibly have been making? XML is always spectaculary inefficient in terms of the size of the document compared to the data it contains. That is a given.
In some cases the benefits of the inherent structure outweighs the verbosity of the format but in your example there is no structure so no advantage. That's why I didn't think you were serious. Wasn't my example though. I too can't see any structural benefit in using XML for a log file. Log files are lists, not trees. Clearly an unclosed XML document containing a great list of log elements isn't a well-formed XML document. If you insist on having an XML based log file, all I suggested was having a single file containg list of XML documents rather than a list of XML elements. That way at least the XML would be well formed.
GreenV8S said:
dern said:
in your example there is no structure
There could be a lot of structure in a log file though, and XML would be a credible way to represent this. Not the most concise format you could imagine, but that isn't always the most important concern.
Oh good! Someone else to argue with. Let's see now ... uhm ... NO!
GreenV8S said:
dern said:
in your example there is no structure
There could be a lot of structure in a log file though, and XML would be a credible way to represent this. Not the most concise format you could imagine, but that isn't always the most important concern.
ATG said:
dern said:
ATG said:
What other point could I possibly have been making? XML is always spectaculary inefficient in terms of the size of the document compared to the data it contains. That is a given.
In some cases the benefits of the inherent structure outweighs the verbosity of the format but in your example there is no structure so no advantage. That's why I didn't think you were serious. Wasn't my example though. I too can't see any structural benefit in using XML for a log file. Log files are lists, not trees. Clearly an unclosed XML document containing a great list of log elements isn't a well-formed XML document. If you insist on having an XML based log file, all I suggested was having a single file containg list of XML documents rather than a list of XML elements. That way at least the XML would be well formed.
As an aside I disagree that log file are by definition lists and not trees... that's just down to how you may have chosen to display them. You could easily create a very useful tree structure log which groups messages together by severity or some other criteria, store that in xml if desired and displayed in a very easy to use hierarchical display tree. You'd be able to easily achieve that with the suggestions I've put forward but not with a solution based on multiple documents.
Well off topic now though.
Edited by dern on Friday 2nd March 13:31
<Reply>
<OriginalTopic>
You could indeed have an XML file with effectively multiple 'root' elements,
but you'd need to have multiple parses of the single file to get the multiple
documents out...
</OriginalTopic>
<EfficiencyDigression relevance="nil">
XML can be pretty verbose and large, but if you add compression it becomes really quite good.
</EfficiencyDigression>
<Edit>
To make the reply well-formed!
</Edit>
</Reply>
<OriginalTopic>
You could indeed have an XML file with effectively multiple 'root' elements,
but you'd need to have multiple parses of the single file to get the multiple
documents out...
</OriginalTopic>
<EfficiencyDigression relevance="nil">
XML can be pretty verbose and large, but if you add compression it becomes really quite good.
</EfficiencyDigression>
<Edit>
To make the reply well-formed!
</Edit>
</Reply>
Edited by Size Nine Elm on Friday 2nd March 13:41
The _only_ point I was making it that there's no need for the boundaries of a file to be the boundaries of an XML document. You can have many XML documents in one file, one XML document spread over several files. The OP blurred the difference between the necessity of having "one root element per file" and "one root element per XML document". Nothing wrong with the former, whereas the latter is a no no.
I wasn't talking about a log file senerio,
It was a business message, Invoice,
Our translation expect 1 root element per physical file,
our supplier sent us a file is multiple root elements in a single file.
so the data we received looks like
<Invoice>Inv Data(hdr,body,vat etc .. )</Invoice>
<Invoice>Inv Data</Invoice>
<Invoice>Inv Data</Invoice>
What we expected is
<Message>
<Invoice>Inv Data</Invoice>
<Invoice>Inv Data</Invoice>
<Invoice>Inv Data</Invoice>
</message>
Now my question is aside from the fact the xml file does not match our xsd,
is the fact they sent multiple root elements in a single physical file correct, I understood is it not valid to do that.
It was a business message, Invoice,
Our translation expect 1 root element per physical file,
our supplier sent us a file is multiple root elements in a single file.
so the data we received looks like
<Invoice>Inv Data(hdr,body,vat etc .. )</Invoice>
<Invoice>Inv Data</Invoice>
<Invoice>Inv Data</Invoice>
What we expected is
<Message>
<Invoice>Inv Data</Invoice>
<Invoice>Inv Data</Invoice>
<Invoice>Inv Data</Invoice>
</message>
Now my question is aside from the fact the xml file does not match our xsd,
is the fact they sent multiple root elements in a single physical file correct, I understood is it not valid to do that.
wikipedia said:
Document element
Every XML document must have exactly one top-level root element (alternatively called the document element), so the following would also be a malformed XML document: it has two top-level thing elements, with no overall parent element encasing them both.
<?xml version="1.0" encoding="UTF-8"?>
<!-- WRONG! NOT WELL-FORMED XML! -->
<thing>Thing one</thing>
<thing>Thing two</thing>
Every XML document must have exactly one top-level root element (alternatively called the document element), so the following would also be a malformed XML document: it has two top-level thing elements, with no overall parent element encasing them both.
<?xml version="1.0" encoding="UTF-8"?>
<!-- WRONG! NOT WELL-FORMED XML! -->
<thing>Thing one</thing>
<thing>Thing two</thing>
Hope this answers your question.
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff



