1

インターネットとさまざまなフォーラムで検索した後、私の答えが見つかりませんでした。
2 つの XSD スキーマで定義された XML ファイルがあります。
XML ファイルを書き込むには、XML ファイルを書き込む方法が 2 つあります:
(XML ファイルを表示するには、"<" 文字を削除する必要があります)
最初に書き込む方法

?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Policy xmlns="http://www.W3C.com/Policy/v3#"  xmlns:ns2="http://www.W3C.com   /PolicyExtension/v3#">
DigestAlg Algorithm="http://test"/>
Transforms>
Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n20010315"></Transform>
/Transforms>
     ns2:Validation>
         ns2:ConditionID>1.0.1</ns2:ConditionID>
         ns2:TConditionID>1.0.2</ns2:TConditionID>
      /ns2:Validation>
/Policy>"  

2番目の方法:

?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Policy xmlns="http://www.W3C.com/Policy/v3#">
DigestAlg Algorithm="http://test"/>
Transforms>
Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n20010315"></Transform>
/Transforms>
     Validation xmlns:ns2="http://www.W3C.com/PolicyExtension/v3#">
         ConditionID>1.0.1</ns2:ConditionID>
         TConditionID>1.0.2</ns2:TConditionID>
      /Validation>
/Policy>  

私のXMLファイルを渡すために、私は使用します:

InputStream doc = new FileInputStream(myXMLFile);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    SchemaFactory sf =     SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    List<Source> sourceListSchema = new ArrayList<Source>();
    sourceListSchema.add(new StreamSource(SignaturePolicy.class.getResourceAsStream(MY_XSD_SCHEMA_1)));
    sourceListSchema.add(new StreamSource(SignaturePolicy.class.getResourceAsStream(MY_XSD_SCHEMA_2)));

    Schema schema;
    try {
        Source[] sourceTmp = new Source[1];
        schema = sf.newSchema(sourceListSchema.toArray(sourceTmp));
    } catch (SAXException e) {
        LogMachine.logger.severe(
                "SAXException : The schema can not be parse :"+e.getMessage());
    }

    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringComments(true);
    dbf.setSchema(schema);

    DocumentBuilder db;
    try {
        db = dbf.newDocumentBuilder();
        documentPolicy = db.parse(Doc); 
    } catch (ParserConfigurationException e) {
        LogMachine.logger.severe(
                "ParserConfigurationException : the file can not be parse by DOM :"+e.getMessage());
    } catch (SAXException e) {
        LogMachine.logger.severe(
                "SAXException : the file can not be parse by DOM :"+e.getMessage());
    } catch (IOException e) {
        LogMachine.logger.severe(
                "IOException : the file can not be open like a file :"+e.getMessage());
    }

このドキュメントを DOM で解析したい場合、最初の XML ファイルにエラーが表示されます

スレッド "main" org.w3c.dom.ls.LSException での例外: 要素 "ns2:Validation" のプレフィックス "ns2" がバインドされていません。

しかし、2 番目の XML ファイルは適切に解析されています。

誰かが2つのドキュメントを解析するのを手伝ってくれる??
助けてくれてありがとう

4

0 に答える 0