dimanche 1 mars 2015

Cant find Syntax error in my program



I have a syntax error in the program below according to Eclispe but I cant seem to find it. Eclipse gives me a Syntax error, insert "}" to complete ClassBody). I am new to Java programming and I don't fully understand the concept of formatting code. Please excuse the incorrect formatting, still learning. Any help would be greatly appreciated.



public class DomParser {


public static void main(String[] args) {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try {
// create a new DocumentBuilderFactory
DocumentBuilder builder = factory.newDocumentBuilder();

// Parse document as an XML File
Document doc = builder.parse("Stuff.xml");

doc.normalize();

//Get rootNode by tagname
NodeList rootNodes = doc.getElementsByTagName("templates");

Node rootNode = rootNodes.item(0);

//Cast rootNode as an Element
Element rootElement = (Element) rootNode;

NodeList templateList = rootElement.getElementsByTagName("template");

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

Node theTemplate = templateList.item(i);

//Cast rootNode as an Element
Element templateElement = (Element) theTemplate;

System.out.println(templateElement.getAttribute("name")+ ".xml");

}

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

// cast each node to a template Element
Node theTemplate = templateList.item(i);

Element templateElement = (Element) theTemplate;

//Get the XML Filename
String fileName = templateElement.getAttribute("name") + ".xml";

// Create a path to the current directory
Path filePath = Paths.get(fileName);

// Create a XML File at the specified Path
Files.createFile(filePath);


try {

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

// root elements
Document doc1 = docBuilder.newDocument();

Element rootElement1 = doc.createElement("template");

doc.appendChild(rootElement1);


// staff elements
Element staff = doc.createElement("templates");

rootElement.appendChild(staff);

Element element = doc.getDocumentElement();

Comment comment = doc.createComment
("Mumbo Jumbo Info"+ "\n<!--$Revision: $:-->"
+ "\n<!-- Created from ERevision: 89474.147");

element.getParentNode().insertBefore(comment, element);

// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

DOMSource source = new DOMSource(doc1);
//StreamResult result = new StreamResult(new File("C:\\file.xml"));


// Output to console for testing
StreamResult result = new StreamResult(System.out );

transformer.transform(source, result);

System.out.println("File" + ":" + fileName + ".xml created");




} catch(FileAlreadyExistsException e) {

} catch(FileNotFoundException e){

} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block

e.printStackTrace();

} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

} catch (TransformerException tfe) {



}


}

}



Aucun commentaire:

Enregistrer un commentaire