This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Forums » Developer » How to make xml data in module?
expandshrink

How to make xml data in module?

How to make xml data in module?

Wednesday 19 November 2003 5:38:03 am - 3 replies

Hi again,

I'm building an import script from AM to eZ and I would like to know how can I insert text data to xml datatype field inside own module?
And also would like to know how to do the same thing with ezauthor datatype.

I've founded how to insert data to normal textfield datatype:
$contentObjectAttributes[0]->setAttribute( 'data_text', $title );
$contentObjectAttributes[0]->store();

But know I could use some help with the xml data...

Thanks again,
Jerry

Wednesday 19 November 2003 11:07:50 am

Its a little more complicated because the string has to be valid xml for the field to work properly.

Heres a code snippet i use to add some a string and some comments. Hope it makes sense happy.gif Emoticon

paul

-----

$contentObjectAttribute =& $contentObjectAttributes[$key];
$contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();

if ($contentClassAttribute->attribute("name"blunk.gif Emoticon == "My attribute"blunk.gif Emoticon
{
$xml = new eZXML();

// Retain the current content of the attribute.
$presentContent=$contentObjectAttribute->attribute("data_text"blunk.gif Emoticon;

// Prepare the original xml for new information.
if ($presentContent != ""blunk.gif Emoticon
{
// Turn this content into a dom tree so we can add to it.
$originalDomTree =& $xml->domTree( $presentContent );

$originalRoot =& $originalDomTree->root();

$commentSeperator =& $originalDomTree->createElementNode( "paragraph" );
$emphasizeDomNode =& $originalDomTree->createElementNode( "emphasize" );
$emphasizeDomNode->appendChild( $originalDomTree->createTextNode( "------- Additional Comment -----------" ) );

$commentSeperator->appendChild($emphasizeDomNode);

$originalRoot->appendChild( $commentSeperator );
}

// Now re-set the attribute with our new comments. Comments
// contains new xml
$contentObjectAttribute->setAttribute("data_text", $comments);

// Turn the new comments into xml by invoking the ez parsing routines.
$xmlText = new eZXMLText( eZXMLTextType::rawXMLText( $contentObjectAttribute ), $contentObjectAttribute );
$input =& $xmlText->attribute( 'input' );
$isValid = $input->validateInput( $http, "ContentObjectAttribute", $contentObjectAttribute );

// Create a new tree with our newly formed xml.
$newDomTree =& $xml->domTree( $contentObjectAttribute->attribute("data_text"blunk.gif Emoticon );
$newRoot =& $newDomTree->root();

// First determine whether we should store the new or original xml.
// Add the new children to the original xml.
if ($presentContent != ""blunk.gif Emoticon
{
if ($newRoot->hasChildren())
{
$rootChildren =& $newRoot->children();
foreach($rootChildren as $child)
{
$originalRoot->appendChild( $child );
}
}

$contentObjectAttribute->setAttribute("data_text", $originalDomTree->toString());
}
else
{
$contentObjectAttribute->setAttribute("data_text", $newDomTree->toString());
}

Wednesday 19 November 2003 11:09:20 am

ack. wish i could use literal to hide the code from the smileys.

jerry, feel free to email me at paul [at] visionwt [dot] com and ill send you the code is use in a better form.

paul

Wednesday 19 November 2003 3:06:24 pm

Hi Paul,

Thank you alot... I haven't tested the script yet, but believe it works like a charm.

I'll try it first from here and will email if it doesn't wan't to work... blunk.gif Emoticon

Once again thank you alot.

Regards,
Jerry

expandshrink

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

36 542 Users on board!

Forums menu

Proudly Developed with from