XML

ファイル例

将棋盤アプリ用の XML ファイルとして、次のデータを使ってください。

  1. shogi1.xml
    
    <?xml version="1.0" encoding="utf-8" standalone="no" ?>
    <!--DOCTYPE shogi SYSTEM "shogi.dtd"-->
    <shogi>
      <koma tejun="sente" name="hi"  mochigoma="true" />
      <koma tejun="sente" name="kin" x="8" y="1" />
      <koma tejun="sente" name="hi" nari="true" x="6" y="2" />
      <koma tejun="sente" name="kei" x="1" y="4" />
      <koma tejun="sente" name="fu" x="1" y="5" />
      <koma tejun="sente" name="fu" x="2" y="5" />
      <koma tejun="sente" name="fu" x="5" y="5" />
      <koma tejun="sente" name="gin" x="8" y="5" />
      <koma tejun="sente" name="fu" x="3" y="6" />
      <koma tejun="sente" name="gin" x="4" y="6" />
      <koma tejun="sente" name="fu" x="7" y="6" />
      <koma tejun="sente" name="fu" x="8" y="6" />
      <koma tejun="sente" name="fu" x="9" y="6" />
      <koma tejun="sente" name="kin" x="5" y="7" />
      <koma tejun="sente" name="gyoku" x="9" y="7" />
      <koma tejun="sente" name="kin" x="8" y="8" />
      <koma tejun="sente" name="kyo" x="1" y="9" />
      <koma tejun="sente" name="kei" x="8" y="9" />
      <koma tejun="sente" name="kyo" x="9" y="9" />
      <koma tejun="gote" name="fu" mochigoma="true" />
      <koma tejun="gote" name="fu" mochigoma="true" />
      <koma tejun="gote" name="fu" mochigoma="true" />
      <koma tejun="gote" name="kyo" x="1" y="1" />
      <koma tejun="gote" name="ou" x="2" y="1" />
      <koma tejun="gote" name="kin" x="4" y="1" />
      <koma tejun="gote" name="fu" x="1" y="2" />
      <koma tejun="gote" name="fu" x="3" y="2" />
      <koma tejun="gote" name="fu" x="2" y="3" />
      <koma tejun="gote" name="kei" x="3" y="3" />
      <koma tejun="gote" name="fu" x="6" y="3" />
      <koma tejun="gote" name="kyo" x="9" y="3" />
      <koma tejun="gote" name="gin" x="3" y="4" />
      <koma tejun="gote" name="fu" x="4" y="4" />
      <koma tejun="gote" name="gin" x="6" y="4" />
      <koma tejun="gote" name="fu" x="9" y="4" />
      <koma tejun="gote" name="kei" x="5" y="5" />
      <koma tejun="gote" name="kaku" x="2" y="6" />
      <koma tejun="gote" name="fu" nari="true" x="7" y="8" />
      <koma tejun="gote" name="kaku" nari="true" x="6" y="9" />
    </shogi>
    
    公益社団法人 日本将棋連盟 詰将棋・次の一手 2017年4月出題分(出題・ 桐山清澄九段)
  2. shogi2.xml
    
    <?xml version="1.0" encoding="utf-8" standalone="no" ?>
    <!DOCTYPE shogi SYSTEM "shogi.dtd">
    <shogi>
      <koma tejun="sente" name="gin"  mochigoma="true" />
      <koma tejun="sente" name="gin" x="5" y="3" />
      <koma tejun="sente" name="kaku" nari="true" x="8" y="5" />
      <koma tejun="gote" mochigoma="restall" />
      <koma tejun="gote" name="gin" x="4" y="1" />
      <koma tejun="gote" name="ou" x="5" y="1" />
      <koma tejun="gote" name="gin" x="6" y="1" />
    </shogi>
    
    Wikipedia 「詰将棋」より

DTD

shogi.dtd


<!ENTITY % tejunlist "(sente | gote)">
<!ENTITY % komalist "(fu | kyo | kei | gin | kin | hi | kaku | ou | gyoku)">
<!ENTITY % bool "(true | false)">
<!ENTITY % mochigomalist "(true | false | restall )">
<!ENTITY % zahyo "(1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)">
<!ELEMENT shogi (koma*)>
<!ELEMENT koma EMPTY>
<!ATTLIST koma
	  tejun %tejunlist; #REQUIRED
	  name %komalist; #IMPLIED
	  nari %bool; #IMPLIED
	  mochigoma %mochigomalist; #IMPLIED
	  x %zahyo; #IMPLIED
	  y %zahyo; #IMPLIED>

CheckDTD.java

DTD による XML ファイルのチェックを行うには、XML ファイルにて DOCTYPE 宣言をしてください。


import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.*;
import javax.xml.parsers.*;
import javax.xml.validation.*;
class TestHandler extends DefaultHandler {
  public TestHandler(){}
  @Override
  public  void error(SAXParseException e) throws SAXException {
    System.out.println(e);
  }
}
class CheckDTD {
  private static SAXParser getSAXParserWDTD() throws Exception {
    final SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    parserFactory.setValidating(true);
    parserFactory.setNamespaceAware(true);
    return parserFactory.newSAXParser();
  }
  public static void main(String[] arg) throws Exception {
    final SAXParser parser = getSAXParserWDTD();
    parser.parse(new FileInputStream("shogi2.xml"),new TestHandler());
  }
}

XML Shema

shogi.xsd


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:annotation>
  </xsd:annotation>
  <xsd:element name="shogi" type="shogiType" />
  <xsd:complexType name="shogiType">
    <xsd:sequence>
      <xsd:element name="koma" type="komaType" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="komaType">
    <xsd:attribute name="tejun" type="tejunType" use="required" />
    <xsd:attribute name="name" type="komaNameType"/>
    <xsd:attribute name="nari" type="xsd:boolean" default="false" />
    <xsd:attribute name="mochigoma" type="mochigomaType" default="false" />
    <xsd:attribute name="x" type="zahyo"/>
    <xsd:attribute name="y" type="zahyo"/>
  </xsd:complexType>
  <xsd:simpleType name="tejunType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="sente" />
      <xsd:enumeration value="gote" />
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:simpleType name="komaNameType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="fu" />
      <xsd:enumeration value="kyo" />
      <xsd:enumeration value="kei" />
      <xsd:enumeration value="gin" />
      <xsd:enumeration value="kin" />
      <xsd:enumeration value="hi" />
      <xsd:enumeration value="kaku" />
      <xsd:enumeration value="ou" />
      <xsd:enumeration value="gyoku" />
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:simpleType name="mochigomaType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="true" />
      <xsd:enumeration value="false" />
      <xsd:enumeration value="restall" />
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:simpleType name="zahyo">
    <xsd:restriction base="xsd:int">
      <xsd:enumeration value="1" />
      <xsd:enumeration value="2" />
      <xsd:enumeration value="3" />
      <xsd:enumeration value="4" />
      <xsd:enumeration value="5" />
      <xsd:enumeration value="6" />
      <xsd:enumeration value="7" />
      <xsd:enumeration value="8" />
      <xsd:enumeration value="9" />
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

CheckXMLSchema.java

XML Schema を使用して XML ファイルをチェックするには DOCTYPE 宣言を外してください。


import java.io.*;
import javax.xml.*;
import javax.xml.parsers.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import javax.xml.validation.*;
class CheckXMLShema {
    private final static String xsdfile="shogi.xsd";
    private final static String xmlfile="shogi1.xml";
    private static Validator getValidator(String xsd) throws Exception {
	final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
	final Schema schema = sf.newSchema(new File(xsd));
	return schema.newValidator();
    }
    private static Document getDocument(InputStream is) throws Exception {
	final DocumentBuilderFactory factory
	    = DocumentBuilderFactory.newInstance();
	final DocumentBuilder builder = factory.newDocumentBuilder();
	return builder.parse(is);
    }
    public static void main(String[] arg) throws Exception {
	final Validator validator = getValidator(xsdfile);
	final Document doc = getDocument(new FileInputStream(xmlfile));
	try {
	    validator.validate(new DOMSource(doc));
	    System.out.println("Document validates fine.");
	} catch (org.xml.sax.SAXException e) {
	    System.out.println("Validation error: " + e.getMessage());
	}
    }
}
[email protected]

坂本直志 <[email protected]>
東京電機大学工学部情報通信工学科