# Parse XML that isn't well formed

**URL:** <https://www.ruby-forum.com/t/parse-xml-that-isnt-well-formed/114970>\
**Category:** Ruby\
**Created:** [September 19, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/parse-xml-that-isnt-well-formed/114970 "2007-09-19T12:05:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [September 19, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/parse-xml-that-isnt-well-formed/114970/1 "2007-09-19T12:05:40Z")

</div>

I have some XML looking like the following, other than being very much  
larger (some files are up to 2GB):

\<?xml version="1.0" encoding="UTF-8"?\>

```
    <server_url>http://myserver.edu/data/</server_url>
    <server_name>myserver.edu</server_name>
    <uploads>
            <result>
                    <dir>/storage/data/results/</dir>
                    <result_name>hadcm3l_00012_00000118_0</result_name>
                    <file_info>
                    <name>hadcm3l_00012_00000118_0_6.zip</name>
                    <nbytes>5154055</nbytes>
                    <md5_checksum>485600296bb601ab4a3d1d49a9fb1c86</md5_checksum>
                    </file_info>
                    <file_info>
                    <name>hadcm3l_00012_00000118_0_7.zip</name>
                    <nbytes>5153055</nbytes>
                    <md5_checksum>36a600296cb60229a3d1d49a9fb1a10</md5_checksum>
                    </file_info>
            </result>
    </uploads>

```

I’ve tried a few xml parsers such as xml-simple, libxml and quixml, but  
all reject this data as badly formed. One answer would, of course, be  
for the data to be re-generated using properly formed xml. Meanwhile, is  
there anything that could be done with the existing files? Is it a case  
of having to write regexps to parse this sort of thing?

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [September 19, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/parse-xml-that-isnt-well-formed/114970/2 "2007-09-19T20:54:00Z")

</div>

On 9/19/07, Milo T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> <file_info>
> </uploads>
> 
> ```

Note that there should be no - the line at the top is a  
declaration, not an opening tag. Where did come from? What  
happens if you remove that from the data?

-A

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [September 20, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/parse-xml-that-isnt-well-formed/114970/3 "2007-09-20T15:27:12Z")

</div>

Alex LeDonne wrote:

> Note that there should be no - the line at the top is a  
> declaration, not an opening tag. Where did come from? What  
> happens if you remove that from the data?

Good point about the XML. Unfortunately, these are the files I have  
received and have to deal with them for now.

Removing the final tag gives:

.file.xml:3: parser error : Extra content at the end of the document  
\<server\_name\>myserver.edu\</server\_name\>  
^  
rake aborted!

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [September 21, 2007, 11:41am UTC](https://www.ruby-forum.com/t/parse-xml-that-isnt-well-formed/114970/4 "2007-09-21T11:41:12Z")

</div>

Jano S. wrote:\>

> You should have done two things: 1. add root node (with  
> closing just before ) AND 2. remove the trailing

Great, thanks.  
That should sort out the “legacy” files, and future ones can be  
corrected.

I have also been parsing each line with IO.foreach and  
/\<(.+)[^\>]\*\>(.+?)\<(/.+)\>/, which though not as nice as a proper XML  
parser does avoid loading huge files into memory in one go.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [September 21, 2007, 11:31am UTC](https://www.ruby-forum.com/t/parse-xml-that-isnt-well-formed/114970/5 "2007-09-21T11:31:14Z")

</div>

On 9/20/07, Milo T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> .file.xml:3: parser error : Extra content at the end of the document  
> \<server\_name\>myserver.edu\</server\_name\>  
> ^  
> rake aborted!

You should have done two things: 1. add root node (with  
closing just before ) AND 2. remove the trailing

Then it’ll be fine.

in your case it’s easy:

data.gsub(‘?\>’, ‘?\>’).gsub(‘’, ‘’)
