The Artima Developer Community
Sponsored Link

Java Answers Forum
append 2 files to a new file.

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Tan Yvette

Posts: 1
Nickname: iooi
Registered: Nov, 2006

append 2 files to a new file. Posted: Nov 9, 2006 11:13 PM
Reply to this message Reply
Advertisement
hellos everyone!

i need to do a java program..
-to parse in 2 .xml files.
-and append both of them into a new file.

i have a set of codes but it don seem to be able to add things into the new-created file.

please help!!

thanks in advance! :D


here's the codes.

__________________________________________________________

import org.w3c.dom.*;
import org.xml.sax.SAXException;
import org.apache.xerces.parsers.DOMParser;
import java.io.*;

public class DOMyufen {

public static Document parseDOM (String sourceFile)
throws Exception {
try {
DOMParser objParser = new DOMParser();
objParser.parse(sourceFile); //Parse the source file
return objParser.getDocument();
} catch (Exception ex) {
System.err.println("Failed with exception: " + ex);
}
return null;
}

public static void printXMLfromDOM (Element element) {

int k;
NamedNodeMap attributes;
NodeList children = element.getChildNodes();
System.out.print("<" + element.getNodeName());
//Start from this element

//print attributes inside the element start tag
attributes = element.getAttributes();
if (attributes != null) {
for (k=0; k<attributes.getLength(); k++) {
System.out.print(" " +
attributes.item(k).getNodeName());
System.out.print("=" +
attributes.item(k).getNodeValue());
}
}
//check for element value or sub-elements
if (element.hasChildNodes()) {
System.out.print(">");

//print all child elements in the DOM tree
for (k=0; k<children.getLength(); k++) {
if (children.item(k).getNodeType() ==
org.w3c.dom.Node.ELEMENT_NODE) {

//printElement((Element)children.item(k));

printXMLfromDOM((Element)children.item(k));
} else if (children.item(k).getNodeType() ==
org.w3c.dom.Node.TEXT_NODE){

System.out.print(children.item(k).getNodeValue());
}
}
//print end tag
System.out.print("</" + element.getNodeName() +">");
} else {
System.out.print("/>"); //element sees to be empty
} //else ends here




} //printXMLfromDom ends here



public static void main (String [] args) throws IOException {

Document document;

try {
document = parseDOM ("cleavableICAT_ms2x2_2dta1.xml");

if (document != null) {
System.out.println("*** XML document ***");
printXMLfromDOM(document.getDocumentElement());
}
else
System.out.println("*** null file ***");
} catch (Exception ex) {}

Document document2;

try {
document2 = parseDOM ("output.2006_10_19_09_35_18.t.xml");

if (document2 != null) {
System.out.println("*** XML document ***");
printXMLfromDOM(document2.getDocumentElement());
}
else
System.out.println("*** null file ***");
} catch (Exception ex) {}

FileWriter fOut = new FileWriter("mergedResult.xml", true);
BufferedOutputStream bf = new BufferedOutputStream(f);
DataOutputStream dos = new DataOutputStream (bf);

dos.writeUTF(document2);

dos.close();



}

_________________________________________________________

end of codes.

Topic: append 2 files to a new file. Previous Topic   Next Topic Topic: Palindrome Problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use