Results 1 to 5 of 5
  1. #1
    DragonGhola is offline Member
    Join Date
    Jun 2011
    Location
    Louisville, KY
    Posts
    6
    Rep Power
    0

    Default 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:
    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");
            }
        }
    If anyone can help me, I'd be very appreciative :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    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.

    only prints the first line of the original file
    I don't see in your code where any lines are printed. Most of the printlns have been commented out.
    Last edited by Norm; 07-22-2011 at 04:53 PM.

  3. #3
    DragonGhola is offline Member
    Join Date
    Jun 2011
    Location
    Louisville, KY
    Posts
    6
    Rep Power
    0

    Default

    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 :)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    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.

  5. #5
    DragonGhola is offline Member
    Join Date
    Jun 2011
    Location
    Louisville, KY
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Java- Writing a file and reading a file line by line
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 07-20-2011, 08:56 AM
  2. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  3. writing to specific line in text file
    By mickmos in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 02:01 PM
  4. Writing To A Specific Text File Line
    By mokonji in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 09:13 PM
  5. Issue with printing line
    By Azndaddy in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2008, 08:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •