Jump to content

Archived

This topic is now archived and is closed to further replies.

ozwolverine

Reading writing XML files to persistent storage

Recommended Posts

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:

[code]
                 
$xml = simplexml_load_file($this->stylesXml);
[/code]

Now I need to add other tags to the already existent tags, So I to get that I wrote this code:
[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);
[/code]


What I want to create is this structure:

[code]
<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>[/code]

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

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

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

After the execution of the line:  [code]$style = $styles -> addChild($styleTag,'');[/code] 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:

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

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

What should I do?

Thanks a lot.

Share this post


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

[code]file_put_contents($apuntador, $xml->asXML());[/code]

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

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

What is wrong in the tags generation code?

thanks a lot,

Share this post


Link to post
Share on other sites

×

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.