Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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 无法使用XSD架构验证XML_Java_Xml_Xsd_Sax - Fatal编程技术网

Java 无法使用XSD架构验证XML

Java 无法使用XSD架构验证XML,java,xml,xsd,sax,Java,Xml,Xsd,Sax,我不熟悉XML验证 我的XSD是 我得到以下例外情况: src-resolve.4.2: Error resolving component 'RootForm'. It was detected that 'RootForm' is in namespace 'http://www.w3schools.com', but components from this namespace are not referenceable from schema document 'file:///X:

我不熟悉XML验证

我的XSD是

我得到以下例外情况:

src-resolve.4.2: Error resolving component 'RootForm'. It was detected that 'RootForm' is in namespace 'http://www.w3schools.com', but components from this namespace are not referenceable from schema document 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'. If this is the incorrect namespace, perhaps the prefix of 'RootForm' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'.
   Public ID: null
   System ID: file:///X:/workspace/XMLValidation/src/xml/transactions.xsd
   Line number: 6
   Column number: 52
   Message: src-resolve.4.2: Error resolving component 'RootForm'. It was detected that 'RootForm' is in namespace 'http://www.w3schools.com', but components from this namespace are not referenceable from schema document 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'. If this is the incorrect namespace, perhaps the prefix of 'RootForm' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'.
cvc-elt.1: Cannot find the declaration of element 'TRANSACTIONS'.
有谁能帮我一下xsd文件中我做错了什么

谢谢
Avnish

您是否也可以共享您正在解析的XML文件? 此外,Trade元素采用的“RecordForm”类型未在模式中定义。 试着把它包括进去看看。
应该导入您正在使用的名称空间,以区分XML中的元素。

以下是我为您提供的正确ans,它肯定会帮助您:

首先,您使用的xml语法不正确,我已在此处更正:

<TRANSACTIONS xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="./transactions.xsd" ASOF_DATE="6/10/2011"  CREATE_DATE="6/10/2011" RECORDS="1769"> 
<TRADE> 
<ACCRUAL_DT>
09/22/2012
</ACCRUAL_DT> 
<COUNTERPARTY_CODE>
US
</COUNTERPARTY_CODE> 
<CUSIP>
BRS87R7N9
</CUSIP> 
<DESC_INSTMT>
CFD COOKSON GROUP PLC
</DESC_INSTMT> 
<DESK/>
</TRADE> 
</TRANSACTIONS>
}


塞尔比解决了

应该导入您正在使用的名称空间,以区分XML中的元素

以下是重用ComplexTypes的XSD:

<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="TRANSACTIONS" type="RootForm" />
  <xsd:complexType name="RootForm">
    <xsd:sequence>
      <xsd:element name="TRADE" type="RecordForm" />
    </xsd:sequence>
    <xsd:attribute name="xsi:noNamespaceSchemaLocation" type="xsd:string" />
    <xsd:attribute name="ASOF_DATE" type="xsd:dateTime" />
    <xsd:attribute name="CREATE_DATE" type="xsd:dateTime" />
    <xsd:attribute name="RECORDS" type="xsd:int" />
  </xsd:complexType>
  <xsd:complexType name="RecordForm">
    <xsd:sequence>
      <xsd:element name="ACCRUAL_DT" type="xsd:dateTime" />
      <xsd:element name="COUNTERPARTY_CODE" type="xsd:string" />
      <xsd:element name="CUSIP" type="xsd:string" />
      <xsd:element name="DESC_INSTMT" type="xsd:string" />
      <xsd:element name="DESK" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>


架构文档中无法引用此命名空间中的组件。请提供并显示您的xml文件。
09/22/2012 US BRS87R7N9 CFD COOKSON GROUP PLC
您尚未在架构中指定事务元素。应该指定它使用RoboForm类型。我在xsd文件中指定了TRANSACTIONS元素,但仍然是相同的异常,非常感谢您。。。使用新的xsd文件,代码现在运行得很好。我将查看我的xsd,找出哪里出了问题,并将在论坛上发布。我的需要是重用复杂类型。但自动生成的xsd正是为了达到这个目的
<TRANSACTIONS xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="./transactions.xsd" ASOF_DATE="6/10/2011"  CREATE_DATE="6/10/2011" RECORDS="1769"> 
<TRADE> 
<ACCRUAL_DT>
09/22/2012
</ACCRUAL_DT> 
<COUNTERPARTY_CODE>
US
</COUNTERPARTY_CODE> 
<CUSIP>
BRS87R7N9
</CUSIP> 
<DESC_INSTMT>
CFD COOKSON GROUP PLC
</DESC_INSTMT> 
<DESK/>
</TRADE> 
</TRANSACTIONS>
package your_package name;
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

public class XMLValidator
{

public void validate() {
    File xmlFile = new File(Provide the xml file location (disk) in double quotes);
    File xsdFile = new File(Provide the xml file location (disk) in double quotes);
    boolean retStat = this.validateSchema(xmlFile, xsdFile);
    if(retStat){    
        System.out.println("Validated");
    }       
    else
        System.out.println("Not Valid");
}

private boolean validateSchema(File xml, File xsd){
    Schema schema = null;
    try{
        schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsd);
    }catch (SAXException e) {
        e.printStackTrace();
        return false;

    }catch(Exception fnEx){
        fnEx.printStackTrace();
        return false;
    }

    if(null != schema){
        Validator validator = schema.newValidator();

        try {
            validator.validate(new StreamSource(xml));
            return true;
        } catch (SAXException e) {
            return false;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return false;
}

public static void main(String args[]){
    XMLValidator newObj=new XMLValidator();
    newObj.validate();
}
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="TRANSACTIONS" type="RootForm" />
  <xsd:complexType name="RootForm">
    <xsd:sequence>
      <xsd:element name="TRADE" type="RecordForm" />
    </xsd:sequence>
    <xsd:attribute name="xsi:noNamespaceSchemaLocation" type="xsd:string" />
    <xsd:attribute name="ASOF_DATE" type="xsd:dateTime" />
    <xsd:attribute name="CREATE_DATE" type="xsd:dateTime" />
    <xsd:attribute name="RECORDS" type="xsd:int" />
  </xsd:complexType>
  <xsd:complexType name="RecordForm">
    <xsd:sequence>
      <xsd:element name="ACCRUAL_DT" type="xsd:dateTime" />
      <xsd:element name="COUNTERPARTY_CODE" type="xsd:string" />
      <xsd:element name="CUSIP" type="xsd:string" />
      <xsd:element name="DESC_INSTMT" type="xsd:string" />
      <xsd:element name="DESK" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>