Archive for the ‘Uncategorized’ Category
Copying nodes between XML documents with Java DOM
Today I had an atomic task of creating a more convenient way to copy nodes from one XML document to another with Java’s DOM implementation. Googling did not help me much in it, so I will share the solution here in case someone would be challenged too.
So, imagine you have multiple XML documents like this:
<document>
<section>
<node attribute="value" />
</section>
</document>
…, another one like this:
<storage><sections /></storage>
… and you wish to read the multilple documents and to put the <section> nodes into the <sections> node of the second one respecting all the structure.
To do that you should:
1. Create the XML document builder:
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
2. Get the <sections> node of the target file:
File target;
Document targetDom = builder.parse(new InputSource(new FileReader(target));
Node targetSections = targetDom.getElementsByTagName("sections").item(0);
// TODO this is not the best way to use that.
// I advice XPath instead.
3. Iterate over the source files and get the <section> nodes:
File[] sources;
for (File source : sources) {
Node sourceSection = builder.parse(new InputSource(new FileReader(source))
.getElementsByTagName("section").item(0);
// continued inside
}
4. Copy the node into the target document:
targetDom.appendChild(targetDom.adoptNode(section.cloneNode(true))); // 'true' means we want to clone children too
5. Write the target back to the file:
TransformerFactory.newInstance().newTransformer().transform(
new DOMSource(targetDom),
new StreamResult(new FileWriter(target))
);
That’s it. Note, this code lacks error handling and probably won’t work directly after copy-paste, but just shows you the usage of the classes.
FW: Up or Out: Solving the IT Turnover Crisis
Web 2.0 Porn Sites run Zend Framework
I never mentioned here, but in my pre-previous developer’s life I was working in a small adult internet company. Right now one of my collegues called me on IM to ask a question about an error he suddenly got fro one of the sites he affiliates to. This is the error:
Fatal error: Uncaught exception ‘Zend_Db_Adapter_Exception’ with message ‘SQLSTATE[00000] [1040] Too many connections’ in /www/zt/root/library/Zend/Db/Adapter/Pdo/Abstract.php:129
Stack trace:
#0 /www/zt/root/library/Zend/Db/Adapter/Abstract.php(216): Zend_Db_Adapter_Pdo_Abstract->_connect()
#1 /www/zt/root/library/Zend/Db/Adapter/Pdo/Abstract.php(204): Zend_Db_Adapter_Abstract->query(‘????SELECT?????…’, Array)
#2 /www/zt/root/mvc/models/User.php(490): Zend_Db_Adapter_Pdo_Abstract->query(‘????SELECT?????…’)
#3 /www/zt/root/mvc/models/User.php(91): User->attachVideosByIp()
#4 /www/zt/root/bootstrap.php(35): User->__construct()
#5 /www/zt/root/index.php(7): require(‘/www/zt/root/bo…’)
#6 {main}
thrown in /www/zt/root/library/Zend/Db/Adapter/Pdo/Abstract.php on line 129
Warning: Unknown: write failed: No space left on device (28) in Unknown on line 0
Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0
BTW, the site (it was Zoo Tube) is not really bad (according to Google cache) and even seems to be a clone of another Web 2.0 porn site – Red Tube, which makes me think ZF is really becomes a trend in real world and and real money projects. Isn’t it amazing?