TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Add xml elements to existing xml file (http://www.talkphp.com/advanced-php-programming/4067-add-xml-elements-existing-xml-file.html)

Ultimatum 03-23-2009 09:18 AM

Add xml elements to existing xml file
 
Normally this shouldn't be a big problem, to add more xml elements in the middle of an already existing xml file. But the problem is that it's an Word xml file which uses Office Open Xml. The problem is that I can't use the normal methods with DOMDocument cause it uses namespaces. So I tried simplexml, but then I couldn't add it in the middle of the text where I needed it but only at the end of the file.

This is a small part of the xml file
Code:

<w:p w:rsidR="000D7961" w:rsidRPr="000D7961" w:rsidRDefault="000D7961" w:rsidP="000D7961">
        <w:pPr>
                <w:pStyle w:val="plattekst2"/>
                <w:spacing w:line="336" w:lineRule="auto"/>
                <w:rPr>
                        <w:rFonts w:ascii="HelveticaNeueLT Std" w:hAnsi="HelveticaNeueLT Std"/>
                        <w:sz w:val="18"/>
                        <w:lang w:val="nl-NL"/>
                </w:rPr>
        </w:pPr>
        <w:bookmarkStart w:id="1" w:name="sponsor"/>
        <w:bookmarkEnd w:id="1"/>
</w:p>

And now I have to replace everything between <w:p> and </w:p> with this, but I have no glue how to do this.
Code:

<w:pPr>
        <w:jc w:val="center"/>
                <w:rPr>
                <w:rFonts w:ascii="Arial" w:hAnsi="Arial"/>
                <w:color w:val="000000"/>
                <w:sz w:val="34"/>
                <w:szCs w:val="34"/>
                </w:rPr>
</w:pPr>
<w:r>
        <w:t xml:space="preserve">Test sponsor</w:t>
</w:r>

Anybody can help me out with this problem?

Salathe 03-23-2009 11:33 AM

To create namespaced elements, use the DOMDocument::createElementNS method.

Ultimatum 03-23-2009 12:37 PM

I tried it, but I already have a namespace, so why do I need to create a new one?
This is the current code I have:
PHP Code:

$oImport = new DOMDocument;
            
$oImport->formatOutput true;
            
$oImport->loadXml('<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
                <w:p>
<w:pPr>
    <w:jc w:val="center"/>
        <w:rPr>
        <w:rFonts w:ascii="Arial" w:hAnsi="Arial"/>
        <w:color w:val="000000"/>
        <w:sz w:val="34"/>
        <w:szCs w:val="34"/>
        </w:rPr>
</w:pPr>
<w:r>
    <w:t xml:space="preserve">Test sponsor</w:t>
</w:r>
            </w:p>
            </w:document>'
);
            
            echo 
"The 'new document' before copying nodes into it:\n";
        
#    echo $oImport->saveXML();
            
            
$oImport->createElementNS('http://schemas.openxmlformats.org/wordprocessingml/2006/main''w''This is the root element!');
            
$node $oImport->getElementsByTagName('w:p')->item(0);
            
            
$oDoc = new DOMDocument;
            
$oDoc->loadXml($this->sDocument);

        
#    echo $bookmark[0]->nodeName;
            
$node $oDoc->importNode($nodetrue);
            
// And then append it to the "<root>" node
            
$oDoc->documentElement->appendChild($node);
        
#    echo "\nThe 'new document' after copying the nodes into it:\n";
            
echo $oDoc->saveXML(); 

But this is the error I get:
Quote:

Catchable fatal error: Argument 1 passed to DOMDocument::importNode() must be an instance of DOMNode, null given, called in C:\Program Files\xampp\htdocs\Webstores-Eindproject\test\word2.php on line 22 and defined in C:\Program Files\xampp\htdocs\Webstores-Eindproject\classes\word\class.reader.php on line 128
This is line 128
PHP Code:

$node $oDoc->importNode($nodetrue); 

So it goes wrong on this line
PHP Code:

$oImport->getElementsByTagName('w:p')->item(0); 

It's logic it goes wrong with the namespace but I have no idea on how to fix it


All times are GMT. The time now is 04:03 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0