Jump to content

Reading writing XML files to persistent storage


ozwolverine

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.
Link to comment
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,
Link to comment
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.