XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 12th, 2008, 05:59 PM
seangamer seangamer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Location: a dark closet ocassionally plagued by flashing lights ;)
Posts: 76 seangamer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 21 m 3 sec
Reputation Power: 1
Unhappy Completely lost - SimpleXML(PHP) entity problem

So here's my problem. I have 3 xml files. The first holds the other two, think of a table of contents, via entities. When I use print_r to display output I receive what I want, plus an extra iteration of the last entity. ??? I don't know why this is happening, my code is below, please help.

main.htm
PHP Code:
<html><head><title></title></head>
<
body>
<?
php
  $xml
=simple_xml_loadfile('main.xml');
  
print_r($xml);
?>
</body>
</html> 


main.xml
PHP Code:
<?xml version="1.0"?>
<!DOCTYPE range [
  <!entity test1 system "../xml/t1/test1.xml">
  <!entity test2 system "../xml/t2/test2.xml">
]>
<range>&test1;&test2;</range>


test1.xml & test2.xml - Just switch all the 1's to 2's
PHP Code:
<section title="Test 1" location="../xml/t1/test1.xml">
  <
page location="test1.php" />
  ...
</
section

Reply With Quote
  #2  
Old September 14th, 2008, 12:35 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Dec 2003
Posts: 1,231 ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 4 Weeks 16 m 57 sec
Reputation Power: 587
Hey, XML is case-sensitive, so how about this?
xml Code:
Original - xml Code
  1. <?xml version="1.0"?>
  2. <!DOCTYPE range [
  3.     <!ENTITY test1 SYSTEM "../xml/t1/test1.xml" >
  4.     <!ENTITY test2 SYSTEM "../xml/t2/test2.xml" >
  5. ]>
  6. <range>&test1;&test2;</range>


Output:
Code:
C:\Work\scrap\test>php -f main.php
SimpleXMLElement Object
(
    [test1] => SimpleXMLElement Object
        (
            [test1] => SimpleXMLElement Object
                (
                    [section] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [title] => Test 1
                                    [location] => ../xml/t1/test1.xml
                                )

                            [page] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [location] => test1.php
                                        )

                                )

                        )

                )

            [test2] => SimpleXMLElement Object
                (
                    [section] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [title] => Test 2
                                    [location] => ../xml/t1/test2.xml
                                )

                            [page] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [location] => test2.php
                                        )

                                )

                        )

                )

        )

    [test2] => SimpleXMLElement Object
        (
            [test2] => SimpleXMLElement Object
                (
                    [section] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [title] => Test 2
                                    [location] => ../xml/t1/test2.xml
                                )

                            [page] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [location] => test2.php
                                        )

                                )

                        )
                )
        )
)

Reply With Quote
  #3  
Old September 14th, 2008, 01:21 PM
seangamer seangamer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Location: a dark closet ocassionally plagued by flashing lights ;)
Posts: 76 seangamer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 21 m 3 sec
Reputation Power: 1
Sorry, this was not directly copied from actual source code, it's for work and they frown on that . This is merely redone from memory, nice catch though . And that is the exact output I get. As you can see there is [test1] and [test2] data in the first [test1] array and then a second separate [test2] array with the [test2] data. There should only be two outputs.

Reply With Quote
  #4  
Old September 14th, 2008, 02:23 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Dec 2003
Posts: 1,231 ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 4 Weeks 16 m 57 sec
Reputation Power: 587
Sorry, I guess I got distracted by the shiny object of run-time errors and totally lost your question/problem!

Weird. Could be a bug in the SimpleXML parser, I guess. I tried it in C# with some pretty basic code, and it seems OK:

c# Code:
Original - c# Code
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4.  
  5. namespace XmlP
  6. {
  7.     class Program
  8.     {
  9.         static void nodeWithAttributes(XmlTextReader reader, int numAttributes)
  10.         {
  11.             Console.Write("<{0} ", reader.Name);
  12.             for (int i = 0; i < numAttributes; ++i)
  13.             {
  14.                 reader.MoveToAttribute(i);
  15.                 Console.Write(@"{0}=""{1}""{2}", reader.Name, reader.Value, i == numAttributes - 1 ? "" : " ");
  16.             }
  17.             Console.WriteLine(">");
  18.         }
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             using (XmlTextReader reader = new XmlTextReader(
  23.                 new FileStream(@"c:\\work\\scrap\\test\\main.xml", FileMode.Open)))
  24.             {
  25.                 reader.EntityHandling = EntityHandling.ExpandEntities;
  26.                 while (reader.Read())
  27.                 {
  28.                     switch (reader.NodeType)
  29.                     {
  30.                         case XmlNodeType.Element:
  31.                             {
  32.                                 if (!reader.HasAttributes)
  33.                                     Console.WriteLine("<{0}>", reader.Name);
  34.                                 else
  35.                                     nodeWithAttributes(reader, reader.AttributeCount);
  36.                             }
  37.                             break;
  38.                         case XmlNodeType.Text:
  39.                             Console.Write(reader.Value);
  40.                             break;
  41.                         case XmlNodeType.XmlDeclaration:
  42.                             Console.WriteLine("<?xml version='1.0'?>");
  43.                             break;
  44.                         case XmlNodeType.Document:
  45.                             break;
  46.                         case XmlNodeType.EndElement:
  47.                             Console.WriteLine("</{0}>", reader.Name);
  48.                             break;
  49.                         default:
  50.                             break;
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }


Output:
xml Code:
Original - xml Code
  1. <?xml version='1.0'?>
  2. <range>
  3. <section title="Test 1" location="../xml/t1/test1.xml">
  4. <page location="test1.php">
  5. </section>
  6. <section title="Test 2" location="../xml/t1/test2.xml">
  7. <page location="test2.php">
  8. </section>
  9. </range>

Reply With Quote
  #5  
Old September 14th, 2008, 04:43 PM
seangamer seangamer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Location: a dark closet ocassionally plagued by flashing lights ;)
Posts: 76 seangamer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 21 m 3 sec
Reputation Power: 1
I've only vaguely looked into c/c++/c# and that was over 3 years ago so I don't remember too much about it However if this works, this may be the route I have to go. Is there anyway to check if it is the parser? I'd hate to add another language to my list at the moment as I'm already in the middle of learning 3 others (PHP, XML, and MySQL) More than willing to look into it later though Thanks for all your help, this is the third forum I've posted this on and you're the only one who's answered thus far. Thank you very much!

Reply With Quote
  #6  
Old September 14th, 2008, 06:45 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Dec 2003
Posts: 1,231 ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 4 Weeks 16 m 57 sec
Reputation Power: 587
Figured it out...provided your version of PHP > 5.1.0 and libxml > 2.6.0:

Calling simplexml_load_file like this:
php Code:
Original - php Code
  1. // Expand entities
  2. $xml=simplexml_load_file('main.xml', NULL, LIBXML_NOENT);


Gave this output:
Code:
ptr2void@home:~/scrap/xmltest> php -f main.php
SimpleXMLElement Object
(
    [section] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [title] => Test 1
                            [location] => ../xml/t1/test1.xml
                        )

                    [page] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [location] => test1.php
                                )

                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [title] => Test 2
                            [location] => ../xml/t2/test2.xml
                        )

                    [page] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [location] => test2.php
                                )

                        )

                )

        )

)

Last edited by ptr2void : September 14th, 2008 at 06:50 PM.

Reply With Quote
  #7  
Old September 14th, 2008, 08:37 PM
seangamer seangamer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Location: a dark closet ocassionally plagued by flashing lights ;)
Posts: 76 seangamer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 21 m 3 sec
Reputation Power: 1
Wow, thank you so much. That is the exact outcome I needed. I have PHP 5.2 at work, though I'm not sure about XML as I didn't have to set anything up for that. I'll try it when I get back to work tomorrow.

If everything goes well this will have saved me months of hard work and finally make my job that cushy desk job I always wanted More time to spend learning more material. Thanks ever so much again!

Reply With Quote
  #8  
Old September 14th, 2008, 08:49 PM
seangamer seangamer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Location: a dark closet ocassionally plagued by flashing lights ;)
Posts: 76 seangamer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 21 m 3 sec
Reputation Power: 1
Oh, one quick other question. I noticed you posted the load file syntax as: simplexml_load_file.
The books and tutorials I have showed it as: simple_xml_loadfile. Was this a typo or is this just an alternative? Thanks again!

Reply With Quote
  #9  
Old September 14th, 2008, 08:50 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Dec 2003
Posts: 1,231 ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level)ptr2void User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 4 Weeks 16 m 57 sec
Reputation Power: 587
Cool, glad I could help! I'm guessing if you're running 5.2, the libxml version against which it was compiled is probably newer than 2.6, as 2.6.0 came out in late 2003. Hopefully no worries for you.

Reply With Quote
  #10  
Old September 15th, 2008, 05:19 PM
seangamer seangamer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Location: a dark closet ocassionally plagued by flashing lights ;)
Posts: 76 seangamer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 21 m 3 sec
Reputation Power: 1
Problem Solved

That did the trick. Thanks again, and you can ignore the other question, I was merely looking at my own code wrong

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Completely lost - SimpleXML(PHP) entity problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT