Department of Mathematics and Computer Science

www.univ-soukahras.dz/en/dept/mi

Module: Bases de données avancées

  1. Information
  2. Questions

How can we put the result that appear at console in a file

2 votes

this is y code 

try {

File file = new File("/C:/Users/workspacememoire12/aaaaa/src/aaaaa/class.xml");
//File file1 = new File("/C:/Users/workspacememoire12/memoire/bin/memoire/class1.xml");
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();

Document doc = dBuilder.parse(file);

System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

if (doc.hasChildNodes()) {

printNote(doc.getChildNodes());

}

} catch (Exception e) {
System.out.println(e.getMessage());
}}
}

private static void printNote(NodeList nodeList) {

for (int count = 0; count < nodeList.getLength(); count++) {

Node tempNode = nodeList.item(count);

// make sure it's element node.
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

// get node name and value
System.out.println("\nNode Name =" + tempNode.getNodeName() + " [OPEN]");
System.out.println("Node Value =" + tempNode.getTextContent());

if (tempNode.hasAttributes()) {

// get attributes names and values
NamedNodeMap nodeMap = tempNode.getAttributes();

for (int i = 0; i < nodeMap.getLength(); i++) {

Node node = nodeMap.item(i);
System.out.println("attr name : " + node.getNodeName());
System.out.println("attr value : " + node.getNodeValue());

}

}

if (tempNode.hasChildNodes()) {

// loop again if has child nodes
printNote(tempNode.getChildNodes());

}

System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]");


try{


PrintWriter out = new PrintWriter(new FileWriter("ommmm.txt"));


out.println( ""+tempNode.getTextContent());
out.close();

PrintStream myconsole = new PrintStream(new File("C:/Users/workspacememoire12/aaaaa/src/class.txt"));
System.setOut(myconsole);
myconsole.print(" Node Name =" + tempNode.getNodeName() + " [CLOSE]");
myconsole.print( "\nNode Name =" + tempNode.getNodeName() + " [OPEN]");

}

catch(FileNotFoundException fx){
System.out.println(fx);} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

}

this code shown the resut in the consol but a need it in File

 

Asked on 14:56, Sunday 8 May 2016 By Meiem Amirat (9 points)
In Bases de données avancées


answers (2)




Answer (1)

-2 votes

public class NewMain1 {

    static FileWriter fw;
    public static void main(String argv[]) {

    try {

    File file = new File("d:/aaa.xml");
        
        fw = new FileWriter("d:/aaa1.xml");
    DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
                             .newDocumentBuilder();

    Document doc = dBuilder.parse(file);

    //System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        fw.write("Root element :" + doc.getDocumentElement().getNodeName());
    if (doc.hasChildNodes()) {

        printNote(doc.getChildNodes());

    }
        fw.flush();

    } catch (Exception e) {
    System.out.println(e.getMessage());
    }

  }

  private static void printNote(NodeList nodeList) throws IOException {

    for (int count = 0; count < nodeList.getLength(); count++) {

    Node tempNode = nodeList.item(count);

    // make sure it's element node.
    if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

        // get node name and value
        //System.out.println("\nNode Name =" + tempNode.getNodeName() + " [OPEN]");
        //System.out.println("Node Value =" + tempNode.getTextContent());
                fw.write("\nNode Name =" + tempNode.getNodeName() + " [OPEN]");
        fw.write("Node Value =" + tempNode.getTextContent());

        if (tempNode.hasAttributes()) {

            // get attributes names and values
            NamedNodeMap nodeMap = tempNode.getAttributes();

            for (int i = 0; i < nodeMap.getLength(); i++) {

                Node node = nodeMap.item(i);
                //System.out.println("attr name : " + node.getNodeName());
                //System.out.println("attr value : " + node.getNodeValue());
                                fw.write("attr name : " + node.getNodeName());
                fw.write("attr value : " + node.getNodeValue());

            }

        }

        if (tempNode.hasChildNodes()) {

            // loop again if has child nodes
            printNote(tempNode.getChildNodes());

        }

        //System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]");
                fw.write("Node Name =" + tempNode.getNodeName() + " [CLOSE]");

    }

    }

  }


}

Answered on 17:40, Friday 20 May 2016 by Mouadh Bekhouche (17 points)
In Bases de données avancées



Answer (2)

1 votes

jjust put those lines in the main 

File file2 = new File("C:\\Users\\ADMIN\\workspace\\MyProject\\file02.xml");
              FileOutputStream fis = new FileOutputStream(file2);
              PrintStream out = new PrintStream(fis);
              System.setOut(out);
           

Answered on 19:05, Saturday 21 May 2016 by bendjebbqr hanane (2 points)
In Bases de données avancées



Do you have an answer ?