举例说明比较容易
一,对于以DTD为Schema的XML文件
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">publicId是-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN
SystemId是http://java.sun.com/dtd/web-app_2_3.dtd二,对于以xsd为Schema的XML文件
<syn:address xmlns:syn="http://www.xxxxxx.com/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.xxxxxx.com/ http://www. xxxxxx.com/address.xsd">publicId是?????????,来谁补充
SystemId是http://www. xxxxxx.com/address.xsdSystemId是用来验证XML文件的Schema存放的地方, 可以想象没底需要的时候去http://java.sun.com/dtd/web-app_2_3.dtd这样的地址去找是不现实和低效的,解决这个问题可以采用象EntityResolver这样的接口来替换在本地搜索
比如public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
{
if (systemId.equals("http://www.syn.com/xxxxxxxxx/address.xsd")) 
{
return new InputSource(new FileInputStream("D:/PROJECT/STUDY/xml/address.xsd"));
} else {
return null;
}
}记着给分

解决方案 »

  1.   

    回答得不错啊!!!再等几天看看!!
    同时再问一下:
    XMLReader reader = XMLReaderFactory.createXMLReader(
    "org.apache.xerces.parsers.SAXParser");
    reader.parse(new InputSource("contents.xml");通过这种方式解析XML文件的时候,会试图到网上下载相关的DTD和XSD,有java.net.ConnectionException异常产生,因为连不上。只能通过像上面那种
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
    {
    if (systemId.equals("http://www.syn.com/xxxxxxxxx/address.xsd")) 
    {
    return new InputSource(new FileInputStream("D:/PROJECT/STUDY/xml/address.xsd"));
    } else {
    return null;
    }
    }
    来解决么???
      

  2.   

    楼上理解的对不过即使能连接上,这种直接采用XML中申明的Schema的做法实际上是很少发生,自己做例子还行。因为当你要验证陌生的XML文档时候,用这个XML中申明的Schema作为标准是件可笑的事(不管它是否可以被快速的获得),就像你问别人他给你的钱是真的吗?他说是真的,你就认为是真的。这个Schema应该通过可信任的手段获得----这是非技术方面的考虑。另外也可以在XML文件内直接写D:/PROJECT/STUDY/xml/address.xsd,这样就可以不用resolveEntity(),这也只是做例子时用用。
      

  3.   

    多谢siyi2005(doudou)老兄,^_^!!决定把所有的分给你啦!!!接着!~