simplexml_load_file() errors.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

simplexml_load_file() errors.

Post by social_experiment »

I wrote a script the uses simplexml_load_file() to parse a file; when parsing a specific file i receive these error messages
Warning: simplexml_load_file() [function.simplexml-load-file]: files/07 Jun/(12) doors_locks_2012_06_06_17_01.xml:8359: parser error : Opening and ending tag mismatch: Question line 8353 and ParentSubjectID

The element in question looks like this

Code: Select all

<Question>
            <Text>Panic Door devices (Weekly):

3. Are all fixings secure, check for wear, are there any visible worn components</Text>
            <QuestionID>qu_011</QuestionID>
            <Index>12</Index>
            <Variable>Q_12</Variable>
            <QType>eqtAmerican</QType>
            <QuestionAnswer>N/A</QuestionAnswer>
            <Answers>
              <Answer>
                <IsOtherSpec>False</IsOtherSpec>
                <Text>N/A</Text>
                <Index>3</Index>
                <AnswerID>5</AnswerID>
                <Code>3</Code>
                <AnswerShortID>an_011_006</AnswerShortID>
                <AnswerVariable>A_qu_011_3</AnswerVariable>
              </Answer>
            </Answers>
          </Question>
My thinking is telling me the problem is happening because of the whitespace inside the <Text> element. Is there any way to deal with this? One answer i found suggests using DOMDocument to load the document first and then using simplexml to parse the document.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: simplexml_load_file() errors.

Post by Celauran »

Cannot duplicate.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: simplexml_load_file() errors.

Post by requinix »

social_experiment wrote:The element in question
Hurr.

The whitespace is fine and I don't see anything wrong. Which lines are 8353 and 8359? Are you passing any libxml options?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: simplexml_load_file() errors.

Post by social_experiment »

Code: Select all

          <Question>
            <Text>Panic Door devices (Weekly):

3. Are all fixings secure, check for wear, are there any visible worn components</Text>
            <QuestionID>qu_011</QuestionID>
            <Index>12</Index>
            <Variable>Q_12</Variable>
Above is line 8353 - 8359. No i'm not using any libxml options;
Celauran wrote:Cannot duplicate.
Could you elaborate on this a bit more?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: simplexml_load_file() errors.

Post by Celauran »

I copy/pasted the XML you posted above, called simplexml_load_file on it, and it worked fine. The issue must be elsewhere in the code.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: simplexml_load_file() errors.

Post by social_experiment »

i see; i did that earlier and couldn't reproduce the problem either. I've decided to try a suggestion i found elsewhere which involves loading the document with loadXML() and the parsing it and from there use simplexml_load_string() as opposed to simplexml_load_file()
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: simplexml_load_file() errors.

Post by social_experiment »

I think i might be dealing with a corrupted xml file. I tested an existing file from a previous export and one i exported this morning and the code does what it is supposed to.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply