TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-23-2009, 09:18 AM   #1 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 13
Thanks: 0
Ultimatum is on a distinguished road
Default 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?

Last edited by Ultimatum : 03-23-2009 at 09:57 AM.
Ultimatum is offline  
Reply With Quote
Old 03-23-2009, 11:33 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

To create namespaced elements, use the DOMDocument::createElementNS method.
Salathe is offline  
Reply With Quote
Old 03-23-2009, 12:37 PM   #3 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 13
Thanks: 0
Ultimatum is on a distinguished road
Default

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

Last edited by Ultimatum : 03-23-2009 at 01:08 PM.
Ultimatum is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy to Modify Login Script with Hierarchical User Permissions and XML Account File Wildhoney Script Giveaway 4 05-04-2011 06:11 AM
Where is my file? superthin General 3 07-25-2009 09:48 AM
Aptana Jaxer and file uploads xenon Advanced PHP Programming 2 06-06-2008 10:22 AM
Writing to XML file buildakicker General 8 02-06-2008 08:17 PM
Using class methods in an included file? Andrew Advanced PHP Programming 6 12-23-2007 03:20 AM


All times are GMT. The time now is 12:06 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design