Append DOM Element twice with JavaScript

A DOM element can have only one direct parent. So if you want to add the same element to multiple containers you need to clone it. This can be done with JavaScript using cloneNode(deep). The variable deep is a boolean value which tells whether to include child elements or not.

Sample:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Append DOM Element twice with JavaScript</title>
    <style type="text/css">
      * { margin: 0; padding: 0; border: 0; }
    </style>
  </head>
  <body>  
    <div id="container1"></div>
    <div id="container2"></div>
    <script type="text/javascript">
      var text = document.createElement('p');
      text.innerHTML = 'Hello World!';
      document.getElementById('container1').appendChild(text);
      document.getElementById('container2').appendChild(text.cloneNode(true));
    </script>
  </body>
</html>

Create a link with an image in JavaScript

With JavaScript you can dynamically create every HTML element that you want. Here is an example on how to create a link with an image:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Create a link with an image in JavaScript</title>
    <style type="text/css">
      .googleImage{ border: 2px solid blue; }
    </style>
  </head>
  <body>  
    <script type="text/javascript">
      // Link
      var link = document.createElement('a');
      link.href = 'http://www.google.de/';
      // Alternative:
      link.setAttribute('href', 'http://www.google.de/');
 
      // Image
      var image = document.createElement('img');
      image.className = 'googleImage';
      image.src = 'http://www.google.de/images/logo.png';
 
      // Create a link with an image inside
      link.appendChild(image);
      // Display the link
      document.body.appendChild(link);
    </script>
  </body>
</html>

Java XML SAX Parser

Um mit Java eine XML Datei zu parsen, empfehle ich einen SAX Parser, da dieser schneller ist als ein DOM Parser. Die Implementierung eines SAX Parsers in Java ist einfach. Man muss nur eine routinemäßige Initialisierung vornehmen und einen eigenen Handler schreiben. Wie das funktioniert, zeigt mein nachfolgendes Code-Beispiel. Ich erkläre ebenfalls, wie man den SAX Parser in Google Android verwenden kann!
Java XML SAX Parser weiterlesen