The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

[SOLVED]Any XML people out there? -- Nothing to see here.

urahonkyurahonky Cynical Old ManRegistered User regular
edited September 2010 in Help / Advice Forum
Okay guys. I need to finish this thing up but I've run into a bump and have NO idea what to do about it. Anyway. I am working on making a validator in Java for my XML file and schema file. For the code our professor has given us a sample one that works for his small, insanely easy XML file. I had to tweak it a little bit, but it works fine. Or so I thought. Once I brought my XML file I was given an error:
Parser encountered a Fatal Error!!!!!!
The markup in the document following the root element must be well-formed.
org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
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:208)
at org.knoesis.Validator.main(Validator.java:74)

Okay... But I thought my XML file was well formed... Here's my XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="transformer.xsl"?>
<auction xmlns="http://knoesis1.wright.edu/~webinfo2010/w100koh" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://knoesis1.wright.edu/~webinfo2010/w100koh schema.xsd">
<computer>
<name>HP Pavillion 2100</name>
<purchase_date>2008</purchase_date>
<current_value>200.00</current_value>
<id_number>com-20100</id_number>
</computer>
<furniture>
<name>Comfy Chair</name>
<purchase_date>2009</purchase_date>
<current_value>80.00</current_value>
<id_number>fur-30110</id_number>
</furniture>
<accessories>
<name>USB Cable</name>
<purchase_date>2010</purchase_date>
<current_value>10.00</current_value>
<id_number>acc-50120</id_number>
</accessories>
</auction>

I even uploaded it to an online validator and it said it was okay. So I'm stumped... Does anyone have any ideas on what I can do to fix this? I'm tired and I want to go to bed. :P

Code for validator:
package org.knoesis;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import com.sun.org.apache.xerces.internal.jaxp.JAXPConstants;

public class Validator {

	public static void main(String[] args) {

		try {
			// Create a factory
			DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
			// set validating features in the factory
			//parsers must be namespace aware to validate
			fact.setNamespaceAware(true);
			fact.setValidating(true);
			
			//since we are a validating against schema  we have to explicitly say that
			try {
				fact.setAttribute(JAXPConstants.JAXP_SCHEMA_LANGUAGE,
						JAXPConstants.W3C_XML_SCHEMA);
			} catch (IllegalArgumentException x) {
				// Happens if the parser does not support JAXP 1.2
				throw new RuntimeException("Cannot use validation here!",x);
			}

			DocumentBuilder builder = fact.newDocumentBuilder();
			
			// using an anonymous inner class to work with the errors
			builder.setErrorHandler(new ErrorHandler() {

				@Override
				public void error(SAXParseException exception)
						throws SAXException {
					System.out.println("Parser encountered an Error!");
					System.out.println(exception.getMessage());
				}

				@Override
				public void fatalError(SAXParseException exception)
						throws SAXException {
					System.out
							.println("Parser encountered a Fatal Error!!!!!!");
					System.out.println(exception.getMessage());

				}

				@Override
				public void warning(SAXParseException exception)
						throws SAXException {
					System.out
							.println("Parser encountered an Warning! Can still proceed");
					System.out.println(exception.getMessage());

				}

			});

			// parse a valid instance
			Document doc = builder.parse(new File(
					"resources/source.xml"));

		} 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();
		}

	}
}

By the way, there aren't any spaces in my XML file because I thought that's what the problem was and I've been too lazy to put them back. :)

urahonky on

Posts

  • RiemannLivesRiemannLives Registered User regular
    edited September 2010
    Sounds like your XML parser doesn't know what to do with a <?xml-stylesheet declaration. You sure that is supported by the library you are using?

    RiemannLives on
    Attacked by tweeeeeeees!
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    You're right, I'm not sure if it is... However I need to have it in there in order to convert the XML to HTML using a transformer (xslt file).

    e: Removing it gave me the same error. Damn. I really wish it was just that. :P

    urahonky on
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    Ugh I figured it out. My XML and schema weren't playing nice with each other. Had to fix it. Thanks anyway guys.

    urahonky on
Sign In or Register to comment.