Jump to content


Photo

Reading writing XML files to persistent storage


  • Please log in to reply
1 reply to this topic

#1 ozwolverine

ozwolverine
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 September 2006 - 02:49 PM

Hello everybody, I have this question:


I need to read (load) an xml file I have on disk, so I'm reading it with something like this:

                  
$xml = simplexml_load_file($this->stylesXml); 

Now I need to add other tags to the already existent tags, So I to get that I wrote this code:
                      //New XML Tags
			$documentContentTag = 'office:document-content'; 
			$stylesTag = 'office:automatic-styles';
			$styleTag = 'style:style';
			$styleProps = 'style:paragraph-properties';
			
			//Attributes (Name , value)
			$styleAttrName = 'style:name';
			$styleAttrVal = 'P1';
			
			$styleAttrFamName = 'style:family';
			$styleAttrFamVal = 'paragraph';
			
			$styleAttrParentName = 'style:parent-style-name';
			$styleAttrParentVal = 'Standard';
			
			$stylePropsAttrName = 'fo:break-before';
			$stylePropsAttrVal = 'page';
			
			//Add tags to existent xml
			$styles = $xml->$documentContentTag -> addChild($stylesTag, '');
			//echo $xml->asXML();

                       $style = $styles -> addChild($styleTag,'');
			
			$style->addAttribute($styleAttrName, $styleAttrVal);
			$style->addAttribute($styleAttrFamName, $styleAttrFamVal);
			$style->addAttribute($styleAttrParentName, $styleAttrParentVal);
			$stylePropsChild = $style->addChild($styleProps,'');
			$stylePropsChild->addAttribute($stylePropsAttrName, $stylePropsAttrVal);


What I want to create is this structure:

<office:automatic-styles>
	<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Standard">
		<style:paragraph-properties fo:break-before="page"/>
	</style:style>
</office:automatic-styles>

in a existent tag (root tag) of the xml called:
</office:document-content>

But I have three problems:
1. I get this warning in the browser:

Warning: SimpleXMLElement::addChild() [function.SimpleXMLElement-addChild]: Node no longer exists in

After the execution of the line: 
$style = $styles -> addChild($styleTag,'');
is executed

Why?

2. If I try to echo the XML file (including tags), I just get literals without tags :(. How could I do it?

3. If I write and XML file using something like:

  if ($this->fp = fopen($fileName, 'w')) {
            $this->fileSize = fwrite($this->fp,$this->content);
            fclose($this->fp);
  }

or read it in a similar way I just get literals without tags

What should I do?

Thanks a lot.


#2 ozwolverine

ozwolverine
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 September 2006 - 04:29 PM

Now I'm able to write the full xml structure to a file, using this code that I found in another post:

file_put_contents($apuntador, $xml->asXML());

Now the problem is generating the XML tags, They throw me that warning message:

Warning: SimpleXMLElement::addChild() [function.SimpleXMLElement-addChild]: Node no longer exists in


What is wrong in the tags generation code?

thanks a lot,





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users