Page 1 of 1

Custom XML Feed

Posted: Wed Apr 03, 2013 2:49 am
by spacebiscuit
I have created a custom XML feed as follows as follows:

Code: Select all

    <content>
    
     <item>
    
      <element type="h1">This is a heading</element>
    
      <element type="p" id="title">This is a title</element>
      
      <element type="p"><strong>Bold text:</strong> this is some text</element>
    
     </item>
    
    </content>
What I want to do is read the xml and out put the html:

Code: Select all

$xml=simplexml_load_file('article.xml');
    	
    foreach ($xml->item as $item){
        
         foreach ($item->element as $element){
        	
               $type=$element['type'];
               $id=$element['id'];
        	  
               echo "<".$type." class=\"".$id."\">".$element."</".$type.">";
        			 	
         }
        	
    }
This works ok if there is only plain text between the tags. But how can I detect tags such as the 'strong' which is wrapped around the last element?

Also consider this div container with an image:

Code: Select all

    <element type="div" id="image-left">
     <img src="image1.jpg">
    </element>
How would I display such content?

Is my approach correct?

Thanks....

Re: Custom XML Feed

Posted: Wed Apr 03, 2013 3:03 am
by requinix
The "id" attribute is special in both HTML and XML. Try to avoid it if you're not actually using it as an identifier.

SimpleXMLElement::asXML() will return the contents of a node as a string. You can use that. Even better would probably be to use CDATA for the node contents:

Code: Select all

    <content>
     <item>
      <element type="h1"><![CDATA[This is a heading]]></element>
      <element type="p" id="title"><![CDATA[This is a title]</element>
      <element type="p"><![CDATA[<strong>Bold text:</strong> this is some text]]></element>
     </item>
    </content>
Then the contents will be just plain text and there's no risk of the HTML interfering with the XML markup.

Re: Custom XML Feed

Posted: Wed Apr 03, 2013 11:23 am
by spacebiscuit
Thanks you, how doe sthat approach help when dealing with different styles of text. In my case I have several blocks of text that need to be wrapped in "<p>" html tags but with various classes.

for example:

<p class="title">some text</p>

<p class="caption">some more text</p>

I'm trying to find a way of storing the style in the feed and extracting it as quickly and as easily as possible. or are you suggesting that I store the HTML tags wrapped around the database in the XML feed itself?

Thanks.

Re: Custom XML Feed

Posted: Wed Apr 03, 2013 12:36 pm
by requinix
Take a step back for a second. Why couldn't you use a simple

Code: Select all

<content>
	<item><![CDATA[<h1>This is a heading</h1><p id="title">This is a title</p><p><strong>Bold text:</strong> this is some text</p>]]></item>
</content>

Re: Custom XML Feed

Posted: Wed Apr 03, 2013 1:29 pm
by spacebiscuit
I thought is was not advised to store raw html? I've most likely misunderstood though if it's being suggested here.

What is the purpose/role of CDATA, if I am storing raw html couldn't I just echo the contents of the feed read element by element into an array?

Thanks.

Re: Custom XML Feed

Posted: Wed Apr 03, 2013 2:54 pm
by requinix
You shouldn't. You should be storing the underlying data that is used to construct that HTML. But as far as I can tell your data is actual HTML. It has the tag name and the class name and the inner HTML content. Unless something will be taking this XML and not reforming the HTML it describes then you might as well just make things easier and store raw HTML. Because that's the end result what you want anyways.
If you're still thinking about it, what's this XML for and what will be using it?

CDATA is an easy way* to put XML-unsafe data into XML. Without it you'd have to htmlspecialchars() the text and get

Code: Select all

<content>
   <item><h1>This is a heading</h1><p id="title">This is a title</p><p><strong>Bold text:</strong> this is some text</p></item>
</content>
which is horrible to look at.

* only if the text doesn't contain a "]]>" - if it does you can't use CDATA

Re: Custom XML Feed

Posted: Wed Apr 03, 2013 4:09 pm
by spacebiscuit
The data is the content of an article which is made up of paragraph and div containers and possibly a table containing images.

Each article can vary in length, some paragraphs have a paticlar styling:

Code: Select all

<p class="title">


I'm just looking for a method to store the entire article in the database and then be able to pull it and extract the html and output.

The challenge is to store the text/image and the formatting or styling associated with it. i'm trying to avoid storing each paragraph in it's own field in the database which i know is not the way to go!

Re: Custom XML Feed

Posted: Wed Apr 03, 2013 5:02 pm
by requinix
The content of the article shouldn't be stored as HTML at all, really. It should be just the paragraphs and tables and such. Consider this forum: posts you write are not stored as HTML* but as the text you actually typed. Which contains BBCode in order to get lists and tables and that kind of fancy formatting. When it comes time to show the post on a page the software only then converts it to HTML*. That's the same approach you should be taking for these articles.

If you can't do that then it sounds like what I said: store actual HTML in there. Paragraphs and divs and tables can go in as-is without needing any work, but if you want to store the images too you'll need to put those in the XML separately. Perhaps with markup like

Code: Select all

<content>
	<item><![CDATA[<p>Here is a smiley face: <img src="icons/smile.gif" alt=":)" /></p>]]></item>
	<images>
		<image src="icons/smile.gif">raw or even better base64-encoded image data here...</image>
	</images>
</content>
But that's only if you really do need to store them in the XML. Try to avoid that.

* Actually they might be, but only as an optimization so the software doesn't have to render the HTML every single time it needs it. It's technically unnormalized but it's one of the accepted exceptions to the rule.

Re: Custom XML Feed

Posted: Thu Apr 04, 2013 10:30 am
by spacebiscuit
So are you hinting that I should use BB Code wrappers? Do you know if there are php functions to wrap and unwrap BB Code or would it be a case of writing my own to interpret and extract the data.

Agreed, I am trying to avoid storing 'formatted' html. I am just looking for the easiest way to store the contents raw, extract and format to screen.

Thanks.

Re: Custom XML Feed

Posted: Thu Apr 04, 2013 1:27 pm
by requinix
You wouldn't have to write your own because it's been done a million times over and you can just Google it. Of course if you want custom markup or tags then you'd have to extend whatever you found.
There's also Wiki-style markup which is a lot different from BBCode but is probably more suited for "article" content.

Re: Custom XML Feed

Posted: Thu Apr 04, 2013 2:02 pm
by spacebiscuit
I just looked and yes I can see there are a few examples of how to strip out code.

However I can't see how BBCode will solve all of my problems, for example in my existing style sheet a sub heading is marked up as:

<p class="sub-head">

I can't see how I can wrap that up in bbcode which is more approriate for html style tabs such as "<strong>" etc,.?

Re: Custom XML Feed

Posted: Thu Apr 04, 2013 2:09 pm
by requinix
I take it that's for a subheading? Wiki markup follows the style of

Code: Select all

=Heading
Content

==Subheading
Subcontent
That is then translated to HTML when it's being displayed.

Code: Select all

<p class="head">Heading</p>
<p>Content</p>

<p class="sub-head">Subheading</p>
<p>Subcontent</p>
You're never actually storing HTML. There is no reference of a <p> or "sub-head" in the article content. Whatever is reading the XML and/or the markup transforms it from the plain text it is to the HTML it wants.

Re: Custom XML Feed

Posted: Fri Apr 05, 2013 7:04 pm
by spacebiscuit
Am I looking at the right mark-up:

http://en.wikipedia.org/wiki/Help:Wiki_markup

I can't see anything there which enables me to reference my own class styles as per your example.

Re: Custom XML Feed

Posted: Sat Apr 06, 2013 6:46 am
by requinix
You wouldn't. You would say that a ==Subheading turns into a <p class="sub-head">Subheading</p>. You don't have to specify the "sub-head" class because it's intrinsic to subheading HTML.

Like I said, if it isn't possible to use BBCode or Wiki markup to do your articles then you're pretty much stuck with storing the raw HTML. Scroll up to a post of mine a few back.