Share » Forums » Developer » How to store HTML text in an XML field

How to store HTML text in an XML field

How to store HTML text in an XML field

Friday 06 October 2006 6:21:42 am - 8 replies

Author Message

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

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu