Jeswant Fernandez
Dom in java
1 Answer(s)      2 years and 3 months ago
Posted in : Java Beginners

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);
  }
}









Related Pages:
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
DOM to SAX and SAX to DOM - XML
Java DOM to SAX and SAX to DOM  Simple API for XML and Document Object Model
DOM - XML
DOM  Hi... I created an xml file through java by using DOM Now..I need to get the prompt for download when i clicked the button... I used... in advance  Hi Friend, Do you want the code in jsp or in java? Please
DOM - XML
DOM  Hello.... I'm creating an xml file from java using DOM... from the database values... and saving it in a file But the created xml file is generating all its files in a single line.. But i need to get the next tag
Java-XML-DOM - XML
Java-XML-DOM  Hi! I need some help. I have to make java program that loads an xml file and from it builds DOM(later i will have to work with it - like using xpath in java find some value and replace it...). Since i'm new to java
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
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
DOM API - Java Beginners
DOM API  How can i go through each and every node of an xml file Each and everyChild? my xml file is given below: 810 32 141 22
DOM API - Java Beginners
DOM API  How can i go through each and every node of an xml file Each and everyChild? my xml file is given below...); } } } ---------------------------------------------- Read for more information. http://www.roseindia.net/xml/dom
DOM
DOM  hello, What is DOM?   hii, DOM is stands for Document object Model. DOM is an interface-oriented Application Programming Interface. We can use it for navigation of the entire document
DOM
DOM  package name for Document class?   Hi Friend, The package is known as dom api. For more information, visit the following links: http://www.roseindia.net/xml/ http://www.roseindia.net/xml/dom/ Thanks
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
DOM Parser Tutorial
DOM Parser Tutorial Introduction Dom stands for Document Object Model. It is a Java API(Application Programming Interface ) for XML Parsing. It is available in package org.w3c.dom. Dom is a cross-language API. It provides many
Java DOM Tutorial
Java DOM Tutorial       This tutorial is complete guide to DOM processing. What is DOM? Document Object Model... through Java. For that we have used DOM parser.   XML Tutorials
Create an Empty DOM Document
Create an Empty DOM Document       This Example shows how to create an empty DOM Document . JAXP (Java API for XML Processing) is an interface which provides parsing of xml
XML DOM Tutorial
XML DOM Tutorial       W3C provides a standard called DOM (Document Object Model) for HTML & XML documents. DOM is also used as an interface for accessing and manipulating HTML & XML
Adding an Attribute in DOM Document
Adding an Attribute in DOM Document       This Example shows you how to adds an attribute in a DOM document. JAXP (Java API for XML Processing) is an interface which provides
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
Example for getElementsByTagName() method in DOM Document.
Example for getElementsByTagName() in DOM Document In this section ,we will discuss the implementation of the getElementsByTagName(), getLength...:\>javac Elementby_TagName c:\>java Elementby_TagName 
Creating Blank DOM Document
Creating Blank DOM Document       This tutorial shows you how to create blank DOM document. JAXP (Java API for XML Processing) is a Java interface that provides a standard approach
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
Implementing the createTextNode() method in DOM API.
Implementing the createTextNode() method in DOM API In this illustration, we... new DOM parsers and  DocumentBuilder is used in code given below for creating an Blank DOM Document. The following code is used to create a textNode. Text
Example of getDocType method in DOM API.
getDocType() Example in DOM API In this section, we will discuss about... parse the xml file using parse() method and create a DOM Tree. Invoke the object...;javac ExampleGetDocType.java C:\>java ExampleGetDocType
Implementing the setAttribute() method of DOM API
Implementing the setAttribute() method of DOM API In this illustration, we... C:\>java setattributeExample <?xml version="1.0...;    </SALE> </orders> Download the java
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
XML Error checker and locater (DOM)
XML Error checker and locater (DOM)   ... and locate (line and column number) an error in your XML document using the DOM APIs... are needed to get a dom parser. All the  parsers have inbuilt capability
DOM importnode, Example of importnode method of document interface.
importNodeExample.java C:\>java importNodeExample <?xml
Java Write To File Dom Document
Java Write To File Dom Document In this tutorial you will learn how to write to file dom document. Write to xml file you would be required to use the package... to Execute this example : After doing the basic process to execute a java program
Getting Dom Tree Elements and their Corresponding XML Fragments
Getting Dom Tree Elements and their Corresponding XML Fragments... will learn to get the elements of a DOM tree and their corresponding XML fragments. Each element of dom tree has a node level starting with '0'. Here
Example of getTagName() method in xml dom api.
the element. In this example, we create the Dom parser by create the instance.... C:\>javac ExamplegetTagName.java C:\>java
DOM to SAX and SAX to DOM - XML
DOM SAX Parser Java  What is the Difference between SAX and DOM parsers
Parsing The XML File using DOM parser in JSP
Parsing The XML File using DOM parser in JSP... the DOM (Document Object Model) parser, that is the platform independent... the processing are displayed on to the present page. DOM parser is not an ideal
Reading xml file using dom parser in java with out using getelementby tag name
Reading xml file using dom parser in java with out using getelementby tag name  Hi, How to read the xml file using java with dom parser, but without... found some thing in the below url : http://www.roseindia.net/answers/viewqa/Java
DOM Parser Example to Parse A DOM Document
DOM Parser Example to Parse A DOM Document In this tutorial, we will discuss about how to parse(retrieve data) DOM Document. The XML DOM views an XML... to create a DOM object.Get a list of Employee elements from the DOM and count
Javascript DOM
generate a fixture list. I was knocking about using DOM techniques but with minimal
javascript dom error-message
javascript dom error-message  javascript dom error-message
DOM Example For Creating a Child Node 
DOM Example For Creating a Child Node  In this section we will discuss about how to add a Child Node in Dom document. The addition of child node is a very logical. To add a child node first we create blank DOM document and then we
DOM paring problem
DOM paring problem  Hi My code always show arrayindexoutofbound here is my code i am using arraylist to display and DOM parser to retrieve XML value. I am searching the value by attributes equals 2. this error i am getting
DOM paring problem
DOM paring problem  Hi My code always show arrayindexoutofbound here is my code i am using arraylist to display and DOM parser to retrieve XML value. I am searching the value by attributes equals 2. this error i am getting
Inserting content(DOM Insertion)
Inserting content(DOM Insertion)       Inserting content(DOM Insertion) Inserting Dom contents to html ,can be categories in the following 4 categories
Creating DOM Child Elements
Creating DOM Child Elements       This lesson shows you how to create root and child elements in the DOM tree. We will first create a blank DOM document and add the root element
Document Object Model(DOM)
Document Object Model(DOM)       The Document Object Model (DOM) is a an standard object model... to use DOM in order to render an HTML document. However, JavaScript scripts
The Document Object Model (DOM) APIs
The Document Object Model (DOM) APIs       The DOM Packages        ... org.w3c.dom This package defines the DOM programming
Pass a dom object from jsp to servlet
Pass a dom object from jsp to servlet  I am creating a dom object in my jsp page. now i want to pass that object in a servlet through calling servlet in jsp. can anyone help me
Creates a New DOM Parse Tree
Creates a New DOM Parse Tree       This Example describes a method to create a new DOM tree .Methods which are used for making a new DOM parse tree are described below :- Element
Read Complex XML using DOM/SAX Parser.
Read Complex XML using DOM/SAX Parser.  I have a XML file which is having two types of Items. I want to extract all details of a type only. My XML goes something like this <Movie> <Bollywood> <Actor> Shah Rukh
Java XML modification
Java XML modification This section explain you how to modify the xml file using DOM parser. For this purpose, you have to use DocumentBuilderFactory, DocumentBuilder and Document classes to get the default DOM parser and parse the xml
Listing nodes used in a document
;  This Example shows you the Lists of nodes used in a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the Document BuilderFactory is used to create new DOM
Locate a Node and Change Its Content
of a node in a DOM document. JAXP (Java API for XML Processing) is an interface... is used to create new DOM parsers. These are some of the methods used in code given... access to the root of the DOM document. NodeList childnodelist

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.