Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JRE 1.5和JDK 1.6时DocumentBuilder.parse中的差异_Java_Xml_Parsing_Sax_Jdk1.6 - Fatal编程技术网

Java 使用JRE 1.5和JDK 1.6时DocumentBuilder.parse中的差异

Java 使用JRE 1.5和JDK 1.6时DocumentBuilder.parse中的差异,java,xml,parsing,sax,jdk1.6,Java,Xml,Parsing,Sax,Jdk1.6,最近,我们终于将我们的项目切换到Java1.6。在执行测试时,我发现使用1.6不会引发使用1.5引发的SAXParseException 下面是我的测试代码来演示这个问题 import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.stream.StreamSou

最近,我们终于将我们的项目切换到Java1.6。在执行测试时,我发现使用1.6不会引发使用1.5引发的SAXParseException

下面是我的测试代码来演示这个问题

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;

import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;


/**
 * Test class to demonstrate the difference between JDK 1.5 to JDK 1.6.
 * 
 * Seen on Linux:
 * 
 * <pre>
 * #java version "1.6.0_18"
 * Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
 * Java HotSpot(TM) Server VM (build 16.0-b13, mixed mode)
 * </pre>
 * 
 * Seen on OSX:
 * 
 * <pre>
 * java version "1.6.0_17"
 * Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-10M3025)
 * Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01-101, mixed mode)
 * </pre>
 * 
 * @author dhiller (creator)
 * @author $Author$ (last editor)
 * @version $Revision$
 * @since 12.03.2010 11:32:31
 */
public class TestXMLValidation {

  /**
   * Tests the schema validation of an XML against a simple schema.
   * 
   * @throws Exception
   *           Falls ein Fehler auftritt
   * @throws junit.framework.AssertionFailedError
   *           Falls eine Unit-Test-Pruefung fehlschlaegt
   */
  @Test(expected = SAXParseException.class)
  public void testValidate() throws Exception {
    final StreamSource schema = new StreamSource( new StringReader( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
      + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" "
      + "elementFormDefault=\"qualified\" xmlns:xsd=\"undefined\">" + "<xs:element name=\"Test\"/>" + "</xs:schema>" ) );
    final String xml = "<Test42/>";
    final DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
    newFactory.setSchema( SchemaFactory.newInstance( "http://www.w3.org/2001/XMLSchema" ).newSchema( schema ) );
    final DocumentBuilder documentBuilder = newFactory.newDocumentBuilder();
    documentBuilder.parse( new InputSource( new StringReader( xml ) ) );
  }

}
导入java.io.StringReader;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.transform.stream.StreamSource;
导入javax.xml.validation.SchemaFactory;
导入org.junit.Test;
导入org.xml.sax.InputSource;
导入org.xml.sax.SAXParseException;
/**
*测试类以演示JDK1.5和JDK1.6之间的差异。
* 
*在Linux上看到:
* 
* 
*#java版本“1.6.0_18”
*Java(TM)SE运行时环境(build 1.6.0_18-b07)
*Java HotSpot(TM)服务器虚拟机(构建16.0-b13,混合模式)
* 
* 
*在OSX上看到:
* 
* 
*java版本“1.6.0_17”
*Java(TM)SE运行时环境(build 1.6.0_17-b04-248-10M3025)
*Java HotSpot(TM)64位服务器虚拟机(构建14.3-b01-101,混合模式)
* 
* 
*@author dhiller(创建者)
*@author$author$(上一个编辑器)
*@version$Revision$
*@自2010年3月12日11:32:31起
*/
公共类TestXMLValidation{
/**
*根据简单模式测试XML的模式验证。
* 
*@抛出异常
*艾因·费勒·奥夫特里特瀑布酒店
*@junit.framework.AssertionFailedError
*Falls eine单元测试Pruefung fehlschlaegt
*/
@测试(预期=SAXParseException.class)
public void testValidate()引发异常{
最终StreamSource架构=新StreamSource(新StringReader(“”)
+ "" + "" + "" ) );
最后一个字符串xml=”“;
final DocumentBuilderFactory newFactory=DocumentBuilderFactory.newInstance();
newFactory.setSchema(SchemaFactory.newInstance(“http://www.w3.org/2001/XMLSchema新闻模式(schema));
final DocumentBuilder DocumentBuilder=newFactory.newDocumentBuilder();
parse(新的InputSource(新的StringReader(xml));
}
}
当使用JVM1.5时,测试通过,在1.6上测试失败,出现“预期异常”

该方法的Javadoc说明:

当用户发现错误时 验证器,解析器负责 向指定的用户报告 ErrorHandler(或者如果错误处理程序 未设置,忽略它们或抛出 它们),就像其他错误一样 由解析器本身发现。换句话说 单词,如果用户指定 ErrorHandler已设置,它必须接收 这些错误,如果不是,它们必须被纠正 按 特定于实现的默认错误 处理规则

该方法的Javadoc说明:

顺便说一句:我尝试通过设置错误处理程序,但仍然没有异常

现在我的问题是:

什么更改为1.6以防止模式验证引发SAXParseException?它是与模式相关还是与我试图解析的xml相关

更新:

以下代码适用于1.5和1.6,正如我所希望的:

@Test(预期=SAXParseException.class)
public void testValidate()引发异常{
最终StreamSource架构=新StreamSource(新StringReader(“”)
+ "" + "" + "" ) );
最后一个字符串xml=”“;
final DocumentBuilderFactory newFactory=DocumentBuilderFactory.newInstance();
最终模式newSchema=SchemaFactory.newInstance(“http://www.w3.org/2001/XMLSchema新闻模式(模式);
newFactory.setSchema(newSchema);
最终验证器newValidator=newSchema.newValidator();
最终来源为=新的StreamSource(新的StringReader(xml));
试一试{
newValidator.validate((源)为);
}
捕获(例外e){
e、 printStackTrace();
投掷e;
}
final DocumentBuilder DocumentBuilder=newFactory.newDocumentBuilder();
parse(新的InputSource(新的StringReader(xml));
}
解决方案似乎是显式使用从模式实例创建的验证器实例。 我找到了解决办法


我仍然不知道为什么…

显然,一个不符合模式的文档只需要默认错误处理程序对stderr进行轻微的谴责。我的解决方案是用更严格的错误处理程序替换默认的错误处理程序:

  @Test(expected = SAXParseException.class)
  public void testValidate() throws Exception {
    final StreamSource schema = new StreamSource( new StringReader( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
      + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" "
      + "elementFormDefault=\"qualified\" xmlns:xsd=\"undefined\">" + "<xs:element name=\"Test\"/>" + "</xs:schema>" ) );
    final String xml = "<Test42/>";
    final DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
    final Schema newSchema = SchemaFactory.newInstance( "http://www.w3.org/2001/XMLSchema" ).newSchema( schema );
    newFactory.setSchema( newSchema );
    final Validator newValidator = newSchema.newValidator();
    final Source is = new StreamSource( new StringReader( xml ) );
    try {
      newValidator.validate( ( Source ) is );
    }
    catch ( Exception e ) {
      e.printStackTrace();
      throw e;
    }
    final DocumentBuilder documentBuilder = newFactory.newDocumentBuilder();
    documentBuilder.parse( new InputSource( new StringReader( xml ) ) );
  }
// builder is my DocumentBuilder
builder.setErrorHandler(new ErrorHandler() {
    @Override
    public void error(SAXParseException arg0) throws SAXException {
        throw arg0;             
    }

    @Override
    public void fatalError(SAXParseException arg0) throws SAXException {
        throw arg0;                 
    }

    @Override
    public void warning(SAXParseException arg0) throws SAXException {
        throw arg0;                 
    }
});