Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Why is this not working? (XML File)
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Snario




PostPosted: Sun May 27, 2012 8:00 pm   Post subject: Why is this not working? (XML File)

Here I have a file that successfully runs. It creates an XML File in .../XMLModify/XMLCreate called XMLCreate.xml

code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlcreate;

import java.io.*;

/**
 *
 * @author Liam Horne
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            OutputStream fout = new FileOutputStream("XMLCreate.xml");
            OutputStream bout = new BufferedOutputStream(fout);
            OutputStreamWriter out = new OutputStreamWriter(bout, "8859_1");

            out.write("<?xml version=\"1.0\" ");
            out.write("encoding=\"ISO-8859-1\"?>\r\n");

            out.write("<program>\r\n");

            out.write("<course>\r\n");
            out.write("<code>ICS4C</code>\r\n");
            out.write("<description>Computer Programming, Grade 12, College</description>\r\n");
            out.write("<teacher>Teacher A</teacher>\r\n");
            out.write("<fileType>Unmodified</fileType>\r\n");
            out.write("</course> \r\n");

            out.write("<course>\r\n");
            out.write("<code>SPH4UI</code>\r\n");
            out.write("<description>Physics, Grade 12, University</description>\r\n");
            out.write("<teacher>Teacher B</teacher>\r\n");
            out.write("<fileType>Unmodified</fileType>\r\n");
            out.write("</course> \r\n");

            out.write("<course>\r\n");
            out.write("<code>MCV4UI</code>\r\n");
            out.write("<description>Calculus & Vectors, Grade 12, University</description>\r\n");
            out.write("<teacher>Teacher C</teacher>\r\n");
            out.write("<fileType>Unmodified</fileType>\r\n");
            out.write("</course> \r\n");

            out.write("</program> \r\n");

            out.flush();  // Don't forget to flush!
            out.close();
        } catch (UnsupportedEncodingException e) {
            System.out.println(
                    "This VM does not support the Latin-1 character set.");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}


.. and then I have this second piece of code that should modify the XML file with a few changes.

code:

package xmlmodify;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.xml.sax.*;

public class Main {
   
    public static void main(String[] args) {
        try{
         String filepath = "C:/Users/Liam Horne/Documents/Organized/ICS4UI/Assignments/Unit 3/A5/XMLModify/XMLCreate/XMLCreate.xml";
         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
         Document doc = docBuilder.parse(filepath);

         //Get the student element by tag name directly
         Node course = doc.getElementsByTagName("course").item(0);

         //append a new node to first course
         Element schoolBoard = doc.createElement("schoolboard");
         schoolBoard.appendChild(doc.createTextNode("WRDSB"));
         course.appendChild(schoolBoard);

         // append a new node to second course
         Node course1 = doc.getElementsByTagName("course").item(1);
         Element schoolBoard1 = doc.createElement("schoolboard");
         schoolBoard1.appendChild(doc.createTextNode("WRDSB"));
         course.appendChild(schoolBoard1);

          // append a new node to third course
         Node course2 = doc.getElementsByTagName("course").item(2);
         Element schoolBoard2 = doc.createElement("schoolboard");
         schoolBoard2.appendChild(doc.createTextNode("WRDSB"));
         course.appendChild(schoolBoard2);

         // change the name of the teachers
         Node a = doc.getElementsByTagName("teacher").item(0);
         a.setTextContent("Mr. Horvath");

         Node b = doc.getElementsByTagName("teacher").item(1);
         a.setTextContent("Mr. Donkers");

         Node c = doc.getElementsByTagName("teacher").item(2);
         a.setTextContent("Mr. Kewley");

     //write the content into xml file
     TransformerFactory transformerFactory = TransformerFactory.newInstance();
     Transformer transformer = transformerFactory.newTransformer();
     DOMSource source = new DOMSource(doc);
     StreamResult result =  new StreamResult(new File(filepath));
     transformer.transform(source, result);

     System.out.println("Done");

   }catch(ParserConfigurationException pce){
         pce.printStackTrace();
   }catch(TransformerException tfe){
         tfe.printStackTrace();
   }catch(IOException ioe){
         ioe.printStackTrace();
   }catch(SAXException sae){
         sae.printStackTrace();
   }
 }
}



However I get this output and the XMLCreate.xml file doesn't change at all.

init:
deps-jar:
Compiling 1 source file to C:\Users\Liam Horne\Documents\Organized\ICS4UI\Assignments\Unit 3\A5\XMLModify\build\classes
compile:
run:
[Fatal Error] XMLCreate.xml:17:24: The entity name must immediately follow the '&' in the entity reference.
org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:180)
at xmlmodify.Main.main(Main.java:18)
BUILD SUCCESSFUL (total time: 1 second)


I don't know what is wrong .. Any help?
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Sun May 27, 2012 8:23 pm   Post subject: RE:Why is this not working? (XML File)

Quote:
The entity name must immediately follow the '&' in the entity reference.


If you look for & in your file. You notice you only use it once as text content. However, your parser seems to be treating this character specially. Thus we should look at the XML specification.

http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML

Oh hey, look, it is treated specially. So it means we should probably be escaping it.
Snario




PostPosted: Sun May 27, 2012 8:43 pm   Post subject: RE:Why is this not working? (XML File)

Thank you, quick response and I located the problem.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: