import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class ParseExamples
{
private final static String COMMON_XML
= "<music>"
+ "<artist name=\"Anderson, Laurie\">"
+ "<album>Big Science</album>"
+ "<album>Strange Angels</album>"
+ "</artist>"
+ "<artist name=\"Fine Young Cannibals\">"
+ "<album>The Raw & The
Cooked</album>"
+ "</artist>"
+ "</music>";
private final static String COMMON_DTD
= "<!ELEMENT music (artist*)>"
+ "<!ELEMENT artist (album+)>"
+ "<!ELEMENT album (#PCDATA)>"
+ "<!ATTLIST artist name CDATA #REQUIRED>";
public static void main(String[] argv)
throws Exception
{
// this version uses just a SYSTEM identifier - note that it gets turned
// into a file: URL
String xml = "<!DOCTYPE music SYSTEM \"bar\">"
+ COMMON_XML;
// this version uses both PUBLIC and SYSTEM identifiers; the SYSTEM ID
// gets munged, the PUBLIC ID
doesn't
// String xml = "<!DOCTYPE music PUBLIC \"foo\" \"bar\">"
// + COMMON_XML;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setEntityResolver(new EntityResolver()
{
public InputSource resolveEntity(String
publicId, String systemId)
throws SAXException, IOException
{
System.out.println("publicId = " + publicId);
System.out.println("systemId = " + systemId);
return new InputSource(new StringReader(COMMON_DTD));
}
});
Document dom = db.parse(new InputSource(new StringReader(xml)));
System.out.println("root element name = " + dom.getDocumentElement().getNodeName());
}
}
Would anyone be able to give me some idea on how to do this?
Search 1000's of available singles in your area at the new Yahoo!7 Dating. Get Started.