- PR -

XML 妥当性検証について

1
投稿者投稿内容
しま太郎
常連さん
会議室デビュー日: 2006/08/22
投稿数: 39
投稿日時: 2007-06-20 17:01
こんにちは。いつもお世話になっております。

現在、xmlファイルの妥当性検証をおこなうため、下記のサイトを参考にしてプログラムを作成しています。

(参考記事)
http://www.itarchitect.jp/xml/-/22646.html

すると、致命的エラーではないもののエラーが発生してしまいます。
同じような現象が起こっている方もいるようなのですが、どうも解決方法が不明です。。
---------------------------------------------------------------
(似たような現象に対してのご質問)
http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=9930&forum=12&1

※"スキーマ文書との関連付けがなされてないようです"というコメントがあるのですが、.setAttribute() でそれも成されていると思います。
---------------------------------------------------------------

何が悪いのか、思いつかれる方がいらっしゃいましたらご教授いただけませんでしょうか。
説明に不備がございましたら申し訳ございません。。

===============================================================
■プログラム

String xsd = "test.xsd";

//DocumentBuilderFactoryのインスタンスを生成
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

//妥当性検証に必要な設定
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xsd);

//DocumentBuilderのインスタンスを生成
DocumentBuilder db = dbf.newDocumentBuilder();

db.setErrorHandler(new ErrorHandler() {

public void error(SAXParseException exception) {

System.out.println(

"Error: URI=" + exception.getSystemId()

+ ", Line=" + exception.getLineNumber()

System.out.println(exception.getMessage());

System.out.println();
}
public void fatalError(SAXParseException exception) {}
public void warning(SAXParseException exception) {}
});

db.parse("test.xml");

str = xceh.getStr(); //エラーがあった場合の内容

〜省略〜

===============================================================
■test.xml
<?xml version="1.0" encoding="UTF-8"?>
<DataList>
<Title>
<AAA>aaa</AAA>
<BBB>bbb</BBB>
<Title>
<Sonota>
<SSS>sss</SSS>
<NNN>nnn</NNN>
</Sonota>
<Sonota>
<Sonota>
<SSS>sss</SSS>
<NNN>nnn</NNN>
</Sonota>
</DataList>

===============================================================
■test.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="DataList" type="List"/>
<xsd:complexType name="List">
<xsd:sequence>
<xsd:element name="Title" type="title_type"/>
<xsd:element maxOccurs="1" minOccurs="0" name="Sonota" type="sontota_type"/>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="optional"/>
</xsd:complexType>

 〜省略〜

===============================================================
■プログラム実行のエラーメッセージ
Line=2
cvc-elt.1: Cannot find the declaration of element 'DataList'.
だっちょ
大ベテラン
会議室デビュー日: 2006/12/05
投稿数: 115
投稿日時: 2007-06-21 09:01
 まず、XMLは正しくありませんね。
<Title>が閉じてないし、<Sonota>が変。

Schemaは途中までなので同じではないですが試したところ
-----------------------
Error: URI=file:///..../test.xsd, Line=6
src-resolve: Cannot resolve the name 'title_type' to a(n) 'type definition' comp
onent.

Error: URI=file:///..../test.xsd, Line=7
src-resolve: Cannot resolve the name 'sontota_type' to a(n) 'type definition' co
mponent.

Error: URI=file:///..../test.xml, Line=11
cvc-complex-type.2.4.d: Invalid content was found starting with element 'Sonota'
. No child element is expected at this point.
-----------------------

となりました。

 そこで、test.xsdのファイル名をtest1.xsdとしてやり直すと、
-----------------------
Error: URI=file:///..../test.xml, Line=2
schema_reference.4: Failed to read schema document 'test.xsd', because 1) could
not find the document; 2) the document could not be read; 3) the root element of
the document is not <xsd:schema>.

Error: URI=file:///..../test.xml, Line=2
cvc-elt.1: Cannot find the declaration of element 'DataList'.
-----------------------
となりました。Schemaのファイル名間違えているのでは?

しま太郎
常連さん
会議室デビュー日: 2006/08/22
投稿数: 39
投稿日時: 2007-06-21 11:05
だっちょさん、ありがとうございました。

見本に出しましたxmlファイルはご指摘の通り間違っておりました・・
スミマセン。。

>Schemaのファイル名間違えているのでは?
ご指摘を受けまして、それまで思いつかなかったのですが相対パスをFileクラスで確認した所、ファイルが存在しないと出ました。。
どうもパスが間違っていたようです。。
自分ではそこが間違えているとは全く思っておらず、見当違いな場所を調べていました・・
訂正後、試した所きちんと検証されるようになりました。
だっちょさん、ご指摘いただきまして誠にありがとうございました。
1

スキルアップ/キャリアアップ(JOB@IT)