Forums / Developer / How to store HTML text in an XML field

How to store HTML text in an XML field

Author Message

Eirik Alfstad Johansen

Friday 06 October 2006 6:21:42 am

Hi,

I want to store some HTML text (consisting of only <BR> tags in addition to text) in a XML field. I had the following code lying around from a 3.5.x compatible extension

$html = str_replace("<BR>", "\n", $html);
				$dummy = "";
	            $converter = new text2xml( $dummy, 0, $contentObjectAttributes[1] );
	            $converter->validateText( $html, $contentObjectAttributes[1] );
	            
	            $contentObjectAttributes[1]->SetAttribute( 'data_int', EZ_XMLTEXT_VERSION_TIMESTAMP );
	            $contentObjectAttributes[1]->store();

However, when I run this under a 3.8.4 installation, I get the following error:

Fatal error: Call to a member function on a non-object in /home/gym2000/public_html/ezpublish-3.8.4/extension/import/kernel/classes/text2xml.php on line 51
Fatal error: eZ publish did not finish its request

Any ideas?

Thanks in advance !

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Claudia Kosny

Friday 06 October 2006 6:48:35 am

Hi Eirik

I can't help you with the error itself but I have copied this from a post somewhere in the forum a couple of month ago and it is working fine for me (EZ 3.8.3). Maybe it helps you as well.

  //creates and stores the content of the contentattribute of type ezxml
  function setEZXMLAttribute( &$contentObjectAttribute, &$attributeContent)
  {
     include_once( 'kernel/classes/datatypes/ezxmltext/handlers/input/ezsimplifiedxmlinputparser.php' );
  
     $contentObjectID = $contentObjectAttribute->attribute('contentobject_id');
     $parser = new eZSimplifiedXMLInputParser( $contentObjectID, false, 0 );
 
     $document = $parser->process($attributeContent);
     if (!is_object($document))
     {
         $this->messages[] = $this->message("Error", "Adding creating EZXMLText");
         return false;
     }
     $domString = eZXMLTextType::domString( $document );
     $contentObjectAttribute->setAttribute( 'data_text', $domString );
     $contentObjectAttribute->store();
  }

Greetings from Luxembourg

Claudia

Eirik Alfstad Johansen

Tuesday 10 October 2006 3:30:30 am

Hi Claudia,

The function worked perfectly! Thanks a lot !

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Xavier Dutoit

Wednesday 11 October 2006 12:48:22 am

Hi

That's the best solution IMO.

However and without being (too) pedentic , it doesn't store html, it convert it into the ezxml format (that later convert it into the internal xml undocumented format). ie if you have some html that isn't compatible with ezxml (font...) then you have problem.

If you want to store real html, use literal+html class (it has been disabled by default on the latest ez versions).

X+

http://www.sydesy.com

Eirik Alfstad Johansen

Monday 20 August 2007 5:23:51 am

Using this code for a new project, I've come across a problem. Allthough links are stored in the ezurl table and appear correctly on the site, no entry is made in the ezurl_object_link table. This results in the URLs not being available in the URL management view.

Any idea how I can ensure that the URLs are added properly?

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

*- pike

Monday 27 August 2007 8:33:21 am

Hi Eirik

I'm successfully using the text2xml class given here
http://ez.no/ezpublish/documentation/development/importing_attribute_data

to import html into a xmlblock. as you can see - if you can read that voodoo - it registers links and linked images too, and returns a nice error/success code. looking a bit deeper, I even see a

$editObject->addContentObjectRelation( $objectID, $editVersion );

and also a

 $linkID =& eZURL::registerURL( $url );

I'm not sure what it is that is needed ... you might just want to use the voodoo blindly :-)

good luck,
*-pike

---------------
The class eZContentObjectTreeNode does.

Eirik Alfstad Johansen

Thursday 27 September 2007 1:54:25 am

Actually, for this latter project, I'm importing XML data (not HTML), and since code originally proposed seemed to work (except for the links) I decided to stick with it.

I did, however, manage to modify it in order to add the appropriate entries in ezurl_object_link. Here's the modified version:

function setEZXMLAttribute( &$contentObjectAttribute, &$attributeContent)
 {
    include_once( 'kernel/classes/datatypes/ezxmltext/handlers/input/ezsimplifiedxmlinputparser.php' );

    $contentObjectID = $contentObjectAttribute->attribute('contentobject_id');
    $parser = new eZSimplifiedXMLInputParser( $contentObjectID, false, 0 );

    $document = $parser->process($attributeContent);

    if (!is_object($document))
    {
        $this->messages[] = $this->message("Error", "Adding creating EZXMLText");
        return false;
    }

	// get links    
    $links =& $document->elementsByName( 'link' );

	// for each link
	foreach($links as $link)
	{
		// create link between url (link) and object
		$eZURLObjectLink = eZURLObjectLink::create( $link->attributeValue('url_id'),
		                   							$contentObjectAttribute->attribute('id'), 
		                   							$contentObjectAttribute->attribute('version') );
		$eZURLObjectLink->store();
	}

    $domString = eZXMLTextType::domString( $document );
    $contentObjectAttribute->setAttribute( 'data_text', $domString );
    $contentObjectAttribute->store();
 }

For those of you following pike's example, please note that the example he refers to is from 2003 and that I belive there has been some changes in the kernel since then. Among them is the attribute name of link ids (previously 'id', now 'url_id').

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Betsy Gamrat

Saturday 29 September 2007 6:34:53 pm

Hi -

I agree with Xavier that the best solution is to use the literal tag with the HTML class.

Another option is to use a text block, and display it with {$node.data_map.text_block.content} (which should display the raw content).

Betsy

Xavier Dutoit

Sunday 30 September 2007 8:18:54 am

While we are at it, do use xmlwash if you go for html literal (or display raw content from a text field).

http://projects.ez.no/xmlwash

X+

http://www.sydesy.com