/    Sign up×
Community /Pin to ProfileBookmark

How do i parse this XML file?

I have tried for so long to understand this XML file to parse it but I cant make it.

[CODE]
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<se:List xmlns:se=”http://www.”>
<se:Event timestamp=”2019-07-01 10:56:19″>
<se:Date endcet=”2019-07-02″ endutc=”2019-07-01″ startcet=”2019-07-02″ startutc=”2019-07-01″ weekdaycet=”1″ weekdayutc=”1″ weeknocet=”27″ weeknoutc=”27″/>
<se:StartTime endcet=”01:25:00″ endutc=”23:25:00″ startcet=”00:40:00″ startutc=”22:40:00″/>
</se:Event>
<se:Event timestamp=”2019-07-01 10:56:20″>
<se:Date endcet=”2019-07-02″ endutc=”2019-07-01″ startcet=”2019-07-02″ startutc=”2019-07-01″ weekdaycet=”1″ weekdayutc=”1″ weeknocet=”27″ weeknoutc=”27″/>
<se:StartTime endcet=”01:55:00″ endutc=”23:55:00″ startcet=”01:25:00″ startutc=”23:25:00″/>
</se:Event>
</se:List>
[/CODE]

[CODE]
$xml = simplexml_load_file(‘file.xml’);
print_r($xml);
[/CODE]

gives the result:
**SimpleXMLElement Object ( )**
I have no idea where to go from there

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJul 19.2019 — It didn't give you anything more than that heading? Are you sure you pointed to the right place/file? And that it is not empty?

The php manual shows an example of exactly what you did and the result of that shows the element that the sample xml file contained. So .....
Copy linkTweet thisAlerts:
@bbq1authorJul 19.2019 — @ginerjm#1606501 no it didnt give me anything more than that. I am 100% sure i pointed to the correct file and the file got over 1000 lines of xml

if I do

[CODE]
$xml = simplexml_load_file('file.xml');
print_r($xml[0]);
[/CODE]

I get:

**SimpleXMLElement Object ( )**

if I do
[CODE]
$xml = simplexml_load_file('file.xml');
print_r($xml[1]);
[/CODE]

I get:

**Warning: main(): Cannot add element List number 1 when only 0 such elements exist in ... ...

SimpleXMLElement Object ( )**


so the data is there....but its not?
Copy linkTweet thisAlerts:
@ginerjmJul 19.2019 — No. It says there are "0 such elements". Sounds 'empty' to me. What happens if you just read in the file as if it were a text file and dump that?

And how about a test on the filename with "if (is_file('file.xml')....."
Copy linkTweet thisAlerts:
@bbq1authorJul 19.2019 — @ginerjm#1606504 do you mean **var_dump(file_get_contents('file.xml'));** ? that gives me **string(1968395) " "**

doing
[CODE]
if (file_exists('file.xml')) {
echo "The file exists";
} else {
echo "The file does not exist";
}
[/CODE]

outputs "The file exists"
Copy linkTweet thisAlerts:
@ginerjmJul 19.2019 — OK. We ruled out error #1.

How about breaking up that line of code into distinct operations?

PS - How big is this xml file? IE, size in bytes/kb/mb.
Copy linkTweet thisAlerts:
@bbq1authorJul 19.2019 — @ginerjm#1606507 what do you mean? I am pretty bad with php

If doing gettype on $xml it says Object

doing strlen on $xml gives 2239

i added an "or die" which does actually point me to the "error"...this is very weird to me
[CODE]
$xml = simplexml_load_file('file.xml') or die('error');
[/CODE]


the file is very big. 1.88MB
Copy linkTweet thisAlerts:
@ginerjmJul 19.2019 — Aha! In the directory listing can you give a better number for its size in bytes? I'm comparing it to that number you showed earlier. Approx. 1.92 mb.
Copy linkTweet thisAlerts:
@bbq1authorJul 19.2019 — @ginerjm#1606509 1 968 395 byte. 1 974 272 byte on disk
Copy linkTweet thisAlerts:
@ginerjmJul 19.2019 — See? That's the same number you got earlier when you combined the read and the dump.
<i>
</i>$data = file_get_contents('file_xml');
var_dump($data);

This is what I meant by using distinct operations.
Copy linkTweet thisAlerts:
@ginerjmJul 19.2019 — So? What happens when you do those two lines of code? I'm hoping you get some real output instead of just a filesize again.
Copy linkTweet thisAlerts:
@SempervivumJul 19.2019 — I struggled some time and finally found this:

https://stackoverflow.com/questions/1575788/php-library-for-parsing-xml-with-a-colons-in-tag-names

This code:

`var_dump($xml-&gt;children('se', true));</C><br/>
gives a meaningful output for your example.<br/>
You can then access attributes this way:<br/>
<C>
var_dump($xml-&gt;children('se', true)-&gt;Event[0]-&gt;Date-&gt;attributes()-&gt;endcet);`
Copy linkTweet thisAlerts:
@ginerjmJul 19.2019 — Does that solve your problem? Cause it didn't solve mine.
Copy linkTweet thisAlerts:
@bbq1authorJul 19.2019 — @Sempervivum#1606513 @ginerjm#1606514 yes that worked for me! thanks alot both of you

and FYI giner. that did only output **string(1968395) " "**
Copy linkTweet thisAlerts:
@ginerjmJul 19.2019 — Interesting that it produces the file size but no message as to not being a proper tool to look at the file with.
×

Success!

Help @bbq1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...