Results 1 to 5 of 5
- 07-22-2011, 03:01 PM #1
Member
- Join Date
- Jun 2011
- Location
- Louisville, KY
- Posts
- 6
- Rep Power
- 0
File Writing Issue - Only Writes First Line
Hi everyone. I have a block of code which is attempting to change a file, then write that file to a new file. The issue is, it seems to be working but only prints the first line of the original file. Here's the code:
If anyone can help me, I'd be very appreciative :)Java Code:public static void writeXmlFile(Document doc, String filename, String pathName, ArrayList<String> bm) { try { Element docEle = dom.getDocumentElement(); //get a nodelist of <employee> elements NodeList nl = docEle.getElementsByTagName("Object"); if (nl != null && nl.getLength() > 0) { //System.out.println("Line 183"); for (int i = 0; i < nl.getLength(); i++) { //get the employee element //System.out.println("Line 186"); Element ele = (Element) nl.item(i); NodeList nl5 = nl.item(i).getChildNodes(); for (int x = 0; x < nl5.getLength(); x++) { if(!nl5.item(x).getNodeName().equals("#text")){ //System.out.println("YAY! GOT HERE!"); //System.out.println(ele.getAttributeNode("Name").getValue()); if (ele.getAttributeNode("Name") != null) { //System.out.println("Line 200"); if (ele.getAttributeNode("Name").getValue().equals("PAGETYPE")) { NodeList n12 = ele.getChildNodes(); for(int g = 0; g < n12.getLength(); g++) { if(!n12.item(g).getNodeName().equals("#text")) { Element f4 = (Element )n12.item(g); if(f4.getAttributeNode("Content").getValue() != null) { if(!f4.getAttributeNode("PageNr").getValue().equals("0")) { int aInt = Integer.parseInt(f4.getAttributeNode("PageNr").getValue()); f4.setNodeValue(bm.get(aInt-1)); } } } } } } } } } } // Prepare the DOM document for writing Source source = new DOMSource(doc); System.out.println("PREPARED FOR WRITING"); // Prepare the output file File file = new File(filename); if (!pathName.equals("")) { File newPath = new File(pathName); boolean success = file.renameTo(new File(newPath, file.getName())); if (!success) { System.out.println("FAILED"); } } Result result = new StreamResult(file); // Write the DOM document to the file System.out.println("SHOULD BE PRINTING!"); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); System.out.println("SHOULD HAVE PRINTED!"); } catch (TransformerConfigurationException e) { System.out.println("Transformer Configuaration Exception - ERROR"); } catch (TransformerException e) { System.out.println("Transformer Exception - ERROR"); } }
- 07-22-2011, 03:50 PM #2
Can you show the contents of the file before and after the program is executed?
Also show what is printed out when the program executes.
I don't see in your code where any lines are printed. Most of the printlns have been commented out.only prints the first line of the original fileLast edited by Norm; 07-22-2011 at 03:53 PM.
- 07-22-2011, 04:00 PM #3
Member
- Join Date
- Jun 2011
- Location
- Louisville, KY
- Posts
- 6
- Rep Power
- 0
Hi, thanks for the quick reply :) Sadly, due to circumstances beyond my control, I can't show any more of the code or document. Suffice to say, the source document is a few pages long at least. I'm sorry if I was ambiguous with my "print" statement, I meant that the xformer.transform() only moved the first line of the source document over to the result file. Thanks for any help :)
- 07-22-2011, 04:36 PM #4
Without an example file and program this will be impossible to test. You'll need to wait until someone that has had your problem comes along.
- 07-22-2011, 04:41 PM #5
Member
- Join Date
- Jun 2011
- Location
- Louisville, KY
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Java- Writing a file and reading a file line by line
By Nazneen Ali in forum New To JavaReplies: 7Last Post: 07-20-2011, 07:56 AM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
writing to specific line in text file
By mickmos in forum New To JavaReplies: 2Last Post: 04-18-2009, 01:01 PM -
Writing To A Specific Text File Line
By mokonji in forum New To JavaReplies: 1Last Post: 03-02-2009, 08:13 PM -
Issue with printing line
By Azndaddy in forum Advanced JavaReplies: 1Last Post: 04-04-2008, 07:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks