次の XML ファイルのようなものがあります。
<credits>
<property name="tag">
<item>v0003</item>
</property>
<property
name="tag">
<item>mhma03h</item>
</property>
</credits>
最初の要件は、この XML が何があっても変更できないことです。以下でそれを行うことを提案しないでください。
これを検証するスキーマを作成する必要があります。そして、検証を行う Java コード。
私は完全に立ち往生しており、何が起こっているのか本当にわかりません。
このようなスキーマはどのようなものですか? 私はそれを手に入れましたが、それはとても悪いので投稿するつもりはありません. :PI は、名前空間を XML 要素に追加する必要はありません。彼らは石に設定されています。
これらすべての要素がパーサーによって「検出」されるようにするにはどうすればよいですか? この名前空間のナンセンスをすべて無視するように言うことはできますか。スキーマに対して検証するこのアプリケーションでは、名前空間の競合はまったく不可能です。
入れてみました
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="books" elementFormDefault="unqualified">
私の名前空間情報と
更新:これまでに与えられた回答を反映するために、私がやっていることを更新しました! :)
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="property">
<xs:complexType>
<xs:sequence>
<xs:element ref="item"/>
</xs:sequence>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="tag"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="mhma03h"/>
<xs:enumeration value="v0003"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="credits">
<xs:complexType>
<xs:sequence>
<xs:element ref="property" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML v0003
ロードして検証するコードはい。質問する前に、ファイルをロードできます。20回くらいチェックしました。:P
SAXParserFactory factory = SAXParserFactory.newInstance();
SchemaFactory schemaFactory =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
factory.setSchema(schemaFactory.newSchema(
new Source[] {new StreamSource("small.xsd")}));
javax.xml.parsers.SAXParser parser = factory.newSAXParser();
org.xml.sax.XMLReader reader = parser.getXMLReader();
reader.setFeature("http://xml.org/sax/features/validation", true);
reader.setFeature("http://apache.org/xml/features/validation/schema", true);
reader.parse(new InputSource("small.xml"));