• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

xml error_please help

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I am stuck below please help

I need to creat an xml with a specific pattern and I have one text file "ids.txt containging the id(1234,1245,1353,1323,5454,9898,8787, eg 10 lines. Now i have to create my xml tags ten times.

Below is the program which i have tried, when i run the firsttime xml is created but next time i get the error like this

An attempt was made to insert a node where it is not permitted. Some one please help to solve my problem

Thanks a lot for your time

Regards
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that what is called "rootElement" isn't actually a root element - you are adding multiple instances of it, and so the document has multiple roots - which is not allowed in XML. Thus the code fails as soon as the second line of the input file is processed.
 
uma prem
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf Dimetter,


Thanks a lot for your response. So is that i have to create one more root element. Can you please suggest me with some sample.

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried? You seem to know the DOM API, so this should not be hard.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@uma prem : as being advised, you've to wrap all the pieces of data as enclosed in record tags into a unique root (the so-called DocumentElement, the one concept seemed lacking there). Let's say the tag called "records". This is a minimal rewrite to illustrate the mental process of doing it. Just replace the corresponding block at the start of the method writeRecord by this.

After tested resulting correct outcome, you can rewrite it to your liking.

ps: You don't want to save the result every time adding one child, do you?! The method as such save it every time which is clearly not an efficient way of doing thing.
 
uma prem
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks both of you for your valuable suggestion

I now understood the concept there should be only one root.
I changed the below lines and can get the output now as I expect

But i have one more thing to do.
I need to pass the line by line value of the textfile(stored in line of my writeRecord()) to be passed to after "record:id:"
How can i do that.

// identifier.appendChild(doc.createTextNode("record:id:"));
Please advise.


Below is my recent prgram
import java.io.*;
import java.util.Calendar;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DeleteXml
{

public static void main(String[] args) throws Exception {
try{
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
doc.setXmlStandalone(true);

Element Identifier=doc.createElement("Identifiers");
doc.appendChild(Identifier);
FileInputStream fis = new FileInputStream("D:\\Output\\del\\ids.txt");
DataInputStream in = new DataInputStream(fis);

BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;

while ((strLine = br.readLine()) != null) {
writeRecord(doc,Identifier,strLine);
System.out.println(strLine);
}

}
catch(ParserConfigurationException pce)
{
pce.printStackTrace();
}
/*catch(TransformerException tfe)
{
tfe.printStackTrace();
}*/
}
public static void writeRecord(Document doc,Element ele,String line)
{
Element rootElement=doc.createElement("record");
ele.appendChild(rootElement);

Element header =doc.createElement("header");
rootElement.appendChild(header);

header.setAttribute("teststatus","active");

Element identifier = doc.createElement("identifier");
identifier.appendChild(doc.createTextNode("record:id:"));

Element datestamp = doc.createElement("Datestamp");
header.appendChild(identifier);
header.appendChild(datestamp);

try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("D:\\testing.xml"));
// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
} catch (Exception e) {
// TODO: handle exception
}
}
}
 
g tsuji
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is wrong with this, as illustration?
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic