Jump to content

Recommended Posts

Hi, I've tried my best to get my head around this but I'm still coming up short

 

I'm trying to get my script to....

  • find the ADVERT tag (which is already in my XML file)
  • Add a new ROOT tag after ADVERT
  • Add the attribute 'LOADTYPE' to ROOT
  • assign the value 'A' to that attribute

The logic of what I've written makes sense in my head but I'm getting a fatal error of undefined method DOMElement::createAttribute()

 

I've tried researching this but I keep find 'simpleXML' references which isn't the method I'm using!

$xmldoc = new DomDocument( '1.0' );
	$xmldoc->preserveWhiteSpace = false;
	$xmldoc->formatOutput = true;

	if( $xml = file_get_contents( '../xml/totaljobs.xml') ) {
	    $xmldoc->loadXML( $xml, LIBXML_NOBLANKS );

	    // find the <ADVERT> tag
	    $advert = $xmldoc->getElementsByTagName('ADVERT')->item(0);

	    // create the <ROOT> tag
	    $root = $xmldoc->createElement('ROOT');

	    // add the new <ROOT> tag
	    $advert->appendChild($root);

	    // add 'LOADTYPE' attribute to <ROOT>
	    $attr1 = $root->createAttribute('LOADTYPE');

            // Set the value for the LOADTYPE attribute
	   $attr1->value = 'A';

           // Append attribute to 
	   $root->appendChild($attr1);

	   // Save the updated xml
	   $xmldoc->save('../xml/totaljobs.xml');

Is createAttribute() the right way of doing this?

 

I'm completely new at XML so this is quite a learning curve.

Edited by paddyfields
Link to comment
https://forums.phpfreaks.com/topic/287591-add-an-new-xml-attribute/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.