Dom in java

Dom in java

Exception in thread "main" org.w3c.dom.DOMException: INVALIDCHARACTERERR: An invalid or illegal XML character is specified. at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createElement(CoreDocumentImpl.java:618) at CreatXMLFile.main(CreatXMLFile.java:29)

View Answers

February 21, 2011 at 11:11 AM

Java Create XML using DOM

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class CreatXMLFile {
  public static void main(String[] args) throws Exception {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter number to add elements in your XML file: ");
    String str = bf.readLine();
    int no = Integer.parseInt(str);
    System.out.print("Enter root: ");
    String root = bf.readLine();
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.newDocument();
    Element rootElement = document.createElement(root);
    document.appendChild(rootElement);
    for (int i = 1; i <= no; i++){
      System.out.print("Enter the element: ");
      String element = bf.readLine();
      System.out.print("Enter the data: ");
      String data = bf.readLine();
      Element em = document.createElement(element);
      em.appendChild(document.createTextNode(data));
      rootElement.appendChild(em);
    }
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result =  new StreamResult(new File("C:/file.xml"));
        transformer.transform(source, result);
  }
}

Ads









Related Tutorials/Questions & Answers:
Dom in java
Dom in java  Exception in thread "main" org.w3c.dom.DOMException...(CoreDocumentImpl.java:618) at CreatXMLFile.main(CreatXMLFile.java:29)   Java Create XML using DOM import java.io.*; import org.w3c.dom.*; import
Java dom from string
Java dom from string  Hi, How to create dom tree from string data? I am trying to search the example for java dom from string. Thanks   Hi, Following code might be useful: import java.io.StringReader; import
Advertisements
XML DOM error - Java Beginners
XML DOM error  import org.w3c.dom.*; import javax.xml.parsers.*; import java.io.*; public class DOMCountElement{ public static void main(String[] args) { try { BufferedReader bf = new BufferedReader(new
java code to create xml document from DOM object
java code to create xml document from DOM object  Hey! After knowing the way we can create DOM objects and add elements to it-> then displaying it on the console ;is there a way I can output the same in xml document
Question about parsing XML document with DOM in JAVA.
Question about parsing XML document with DOM in JAVA.  Hi, I need to parse a XML file witch has info about classes. So i have for each class to have its methods and for each method to get its parameters. I'm trying to do
dom to xml string
dom to xml string  Hi, I am writing a program in java that reads... have save the dom document into xml file. How to convert dom to xml string... for converting DOM object into string data. Please check the thread dom to xml
Maven dependency for org.webjars.npm - react-dom version 15.7.0 is released. Learn to use react-dom version 15.7.0 in Maven based Java projects
this version ( org.webjars.npm - react-dom version 15.7.0 ) in their Java... and includes  org.webjars.npm - react-dom version 15.7.0 java library...Maven dependency for  org.webjars.npm  - Version 15.7.0 of react-dom
Maven dependency for org.webjars.npm - react-dom version 17.0.2 is released. Learn to use react-dom version 17.0.2 in Maven based Java projects
this version ( org.webjars.npm - react-dom version 17.0.2 ) in their Java... and includes  org.webjars.npm - react-dom version 17.0.2 java library...Maven dependency for  org.webjars.npm  - Version 17.0.2 of react-dom
Maven dependency for org.webjars.npm - react-dom version 0.14.0 is released. Learn to use react-dom version 0.14.0 in Maven based Java projects
this version ( org.webjars.npm - react-dom version 0.14.0 ) in their Java... and includes  org.webjars.npm - react-dom version 0.14.0 java library...Maven dependency for  org.webjars.npm  - Version 0.14.0 of react-dom
Maven dependency for org.webjars.npm - react-dom version 15.3.0 is released. Learn to use react-dom version 15.3.0 in Maven based Java projects
this version ( org.webjars.npm - react-dom version 15.3.0 ) in their Java... and includes  org.webjars.npm - react-dom version 15.3.0 java library...Maven dependency for  org.webjars.npm  - Version 15.3.0 of react-dom
Maven dependency for org.webjars.npm - react-dom version 15.2.0 is released. Learn to use react-dom version 15.2.0 in Maven based Java projects
this version ( org.webjars.npm - react-dom version 15.2.0 ) in their Java... and includes  org.webjars.npm - react-dom version 15.2.0 java library...Maven dependency for  org.webjars.npm  - Version 15.2.0 of react-dom
Maven dependency for batik - batik-dom version 1.6 is released. Learn to use batik-dom version 1.6 in Maven based Java projects
- batik-dom version 1.6 in Java projects. Follow the step by step tutorial for using...Maven dependency for  batik  - Version 1.6 of batik-dom released The developers of   batik - batik-dom project have released the latest
Maven dependency for org.webjars.npm - react-dom version 16.3.0 is released. Learn to use react-dom version 16.3.0 in Maven based Java projects
this version ( org.webjars.npm - react-dom version 16.3.0 ) in their Java... and includes  org.webjars.npm - react-dom version 16.3.0 java library...Maven dependency for  org.webjars.npm  - Version 16.3.0 of react-dom
Maven dependency for org.webjars.npm - react-dom version 16.7.0-alpha.0 is released. Learn to use react-dom version 16.7.0-alpha.0 in Maven based Java projects
; org.webjars.npm - react-dom version 16.7.0-alpha.0 in Java projects. Follow...; org.webjars.npm - react-dom version 16.7.0-alpha.0 java library in your project... of react-dom released The developers of   org.webjars.npm - react-dom
Maven dependency for org.webjars.npm - react-dom version 15.6.2 is released. Learn to use react-dom version 15.6.2 in Maven based Java projects
this version ( org.webjars.npm - react-dom version 15.6.2 ) in their Java... and includes  org.webjars.npm - react-dom version 15.6.2 java library...Maven dependency for  org.webjars.npm  - Version 15.6.2 of react-dom
Maven dependency for org.webjars.npm - react-dom version 16.3.1 is released. Learn to use react-dom version 16.3.1 in Maven based Java projects
this version ( org.webjars.npm - react-dom version 16.3.1 ) in their Java... and includes  org.webjars.npm - react-dom version 16.3.1 java library...Maven dependency for  org.webjars.npm  - Version 16.3.1 of react-dom
Maven dependency for batik - batik-dom version 1.5 is released. Learn to use batik-dom version 1.5 in Maven based Java projects
- batik-dom version 1.5 in Java projects. Follow the step by step tutorial for using...Maven dependency for  batik  - Version 1.5 of batik-dom released The developers of   batik - batik-dom project have released the latest
Java XML Parsing Using DOM
Java XML Parsing Using SAX To Parse XML document using SAX parser method you need to follow the following steps. Consider the following XML file... java class is given below based on above explanation XmlParsingUsingSax.java
Maven dependency for org.apache.wss4j - wss4j-ws-security-dom version 2.3.0 is released. Learn to use wss4j-ws-security-dom version 2.3.0 in Maven based Java projects
-security-dom version 2.3.0 ) in their Java project if it is based on Maven...-security-dom version 2.3.0 java library in your project. Now you can save...-security-dom released The developers of   org.apache.wss4j - wss4j-ws
Maven dependency for org.apache.wss4j - wss4j-ws-security-dom version 2.2.7 is released. Learn to use wss4j-ws-security-dom version 2.2.7 in Maven based Java projects
-security-dom version 2.2.7 ) in their Java project if it is based on Maven...-security-dom version 2.2.7 java library in your project. Now you can save...-security-dom released The developers of   org.apache.wss4j - wss4j-ws
Maven dependency for org.apache.wss4j - wss4j-ws-security-dom version 2.4.0 is released. Learn to use wss4j-ws-security-dom version 2.4.0 in Maven based Java projects
-security-dom version 2.4.0 ) in their Java project if it is based on Maven...-security-dom version 2.4.0 java library in your project. Now you can save...-security-dom released The developers of   org.apache.wss4j - wss4j-ws
Maven dependency for io.fluidsonic.react - fluid-react-dom version 0.9.1 is released. Learn to use fluid-react-dom version 0.9.1 in Maven based Java projects
? Steps to use  io.fluidsonic.react - fluid-react-dom version 0.9.1 in Java...-react-dom released The developers of   io.fluidsonic.react - fluid-react-dom project have released the latest version of this library on 08 Feb 2021
Maven dependency for io.fluidsonic.react - fluid-react-dom version 0.9.4 is released. Learn to use fluid-react-dom version 0.9.4 in Maven based Java projects
? Steps to use  io.fluidsonic.react - fluid-react-dom version 0.9.4 in Java...-react-dom released The developers of   io.fluidsonic.react - fluid-react-dom project have released the latest version of this library on 17 Feb 2021
Maven dependency for io.fluidsonic.react - fluid-react-dom version 0.9.7 is released. Learn to use fluid-react-dom version 0.9.7 in Maven based Java projects
? Steps to use  io.fluidsonic.react - fluid-react-dom version 0.9.7 in Java...-react-dom released The developers of   io.fluidsonic.react - fluid-react-dom project have released the latest version of this library on 08 Mar 2021
Maven dependency for io.fluidsonic.react - fluid-react-dom version 0.9.10 is released. Learn to use fluid-react-dom version 0.9.10 in Maven based Java projects
-dom version 0.9.10 ) in their Java project if it is based on Maven and Gradle...? Steps to use  io.fluidsonic.react - fluid-react-dom version 0.9.10 in Java...-react-dom released The developers of   io.fluidsonic.react - fluid
Maven dependency for io.fluidsonic.react - fluid-react-dom version 0.11.0 is released. Learn to use fluid-react-dom version 0.11.0 in Maven based Java projects
-dom version 0.11.0 ) in their Java project if it is based on Maven and Gradle...? Steps to use  io.fluidsonic.react - fluid-react-dom version 0.11.0 in Java...-react-dom released The developers of   io.fluidsonic.react - fluid
Maven dependency for org.apache.wss4j - wss4j-ws-security-dom version 2.4.1 is released. Learn to use wss4j-ws-security-dom version 2.4.1 in Maven based Java projects
-security-dom version 2.4.1 ) in their Java project if it is based on Maven...-security-dom version 2.4.1 java library in your project. Now you can save...-security-dom released The developers of   org.apache.wss4j - wss4j-ws
Maven dependency for org.apache.wss4j - wss4j-ws-security-dom version 2.3.2 is released. Learn to use wss4j-ws-security-dom version 2.3.2 in Maven based Java projects
-security-dom version 2.3.2 ) in their Java project if it is based on Maven...-security-dom version 2.3.2 java library in your project. Now you can save...-security-dom released The developers of   org.apache.wss4j - wss4j-ws
Maven dependency for org.apache.wss4j - wss4j-ws-security-dom version 2.2.5 is released. Learn to use wss4j-ws-security-dom version 2.2.5 in Maven based Java projects
-security-dom version 2.2.5 ) in their Java project if it is based on Maven...-security-dom version 2.2.5 java library in your project. Now you can save...-security-dom released The developers of   org.apache.wss4j - wss4j-ws
Maven dependency for org.apache.wss4j - wss4j-ws-security-dom version 2.2.6 is released. Learn to use wss4j-ws-security-dom version 2.2.6 in Maven based Java projects
-security-dom version 2.2.6 ) in their Java project if it is based on Maven...-security-dom version 2.2.6 java library in your project. Now you can save...-security-dom released The developers of   org.apache.wss4j - wss4j-ws
Maven dependency for org.apache.wss4j - wss4j-ws-security-dom version 2.3.1 is released. Learn to use wss4j-ws-security-dom version 2.3.1 in Maven based Java projects
-security-dom version 2.3.1 ) in their Java project if it is based on Maven...-security-dom version 2.3.1 java library in your project. Now you can save...-security-dom released The developers of   org.apache.wss4j - wss4j-ws

Ads