- NekoHtml provides html reading,document fragment creating,scanner.
- It can fix up the error in html file like missing tags.
- The NekoHtml library jar file can be downloaded :-
Project Structure:-
Creating Document Fragment:-
- DocumentFragment is a class for creating new node adding it into Live DOM.
NekoHtmlDemo.java
package com.sandeep.neko.html;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class NekoHtmlDemo {
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException, TransformerException {
/* Make InputSource file from the html file */
File htmlFile = new File("./demoHtmlInput.html");
InputStream inStrem = new FileInputStream(htmlFile);
InputSource ins = new InputSource(inStrem);
/* Make a Document File */
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document htmlDocument = factory.newDocumentBuilder().parse(ins);
/* Create a new div node */
Element root = htmlDocument.getDocumentElement();
System.out.print("root Name : " + root.getTagName() + "n");
NodeList nodeList = root.getElementsByTagName("div");
Node aDivNode = nodeList.item(0);
System.out.print("Div Content :" + aDivNode.getTextContent() + "n");
/* Attach a text node to div node, i.e,Hi Surabhi Here*/
Element anewDivElement = htmlDocument.createElement("div");
Text txtNode = htmlDocument.createTextNode("Hi Surabhi Here");
anewDivElement.appendChild(txtNode);
/* Attach the new div elemnet to document Fragment */
DocumentFragment fragment = htmlDocument.createDocumentFragment();
fragment.appendChild(anewDivElement);
aDivNode.appendChild(fragment);
/* Create a Transformer Factory and a Transformer */
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer htmlTransformer = tFactory.newTransformer();
Source domSource = new DOMSource(htmlDocument);
FileWriter filewriter = new FileWriter(
new File("./demoHtmlOutPut.html"));
Result result = new StreamResult(filewriter);
htmlTransformer.transform(domSource, result);
}
}
Output:-
- A new File “demoFileOutput.html” is created wit a div node attached.
NekoHtml Parser:-
package com.sandeep.neko.html;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.xerces.dom.DocumentImpl;
import org.cyberneko.html.parsers.DOMFragmentParser;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class NekoHtmlDemo {
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException, TransformerException {
DOMFragmentParser parser = new DOMFragmentParser();
parser.setProperty("http://cyberneko.org/html/properties/names/elems","lower");
parser.setFeature("http://cyberneko.org/html/features/document-fragment",true);
Document document = new DocumentImpl();
DocumentFragment fragment = document.createDocumentFragment();
// parse the document into a fragment
parser.parse(new InputSource(new StringReader("Sandeep
abcd")), fragment);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty("omit-xml-declaration", "yes");
DOMSource source = new DOMSource(fragment);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
}
}
Output:-
Sandeep
abcdCode Download:-
download neko html demo