聽1銆佸垱寤篨ML鏂囨。錛?br />聽import聽java.io.*;
聽import聽org.jdom.*;
聽import聽org.jdom.input.*;
聽import聽org.jdom.output.*;
聽public聽class聽CreateXML
聽{
聽聽public聽void聽Create()
聽聽{
聽聽聽try
聽聽聽{
聽聽聽聽Document聽doc聽=聽new聽Document();聽聽聽
聽聽聽聽ProcessingInstruction聽pi=new聽ProcessingInstruction("xml-stylesheet","type="text/xsl"聽href="test.xsl"");
聽聽聽聽doc.addContent(pi);
聽聽聽聽
聽聽聽聽Namespace聽ns聽=聽Namespace.getNamespace("http://www.bromon.org" );
聽聽聽聽Namespace聽ns2聽=聽Namespace.getNamespace("other",聽"http://www.w3c.org" );
聽聽聽聽Element聽root聽=聽new聽Element("鏍瑰厓绱?,聽ns);
聽聽聽聽root.addNamespaceDeclaration(ns2);
聽聽聽聽doc.setRootElement(root);
聽聽聽聽Element聽el1聽=聽new聽Element("鍏冪礌涓");
聽聽聽聽el1.setAttribute("灞炴?,聽"灞炴т竴");
聽聽聽聽
聽聽聽聽Text聽text1=new聽Text("鍏冪礌鍊?);
聽聽聽聽聽聽聽聽聽聽聽聽聽Element聽em聽=聽new聽Element("鍏冪礌浜?).addContent("絎簩涓厓绱?);
聽聽聽聽el1.addContent(text1);
聽聽聽聽聽聽聽聽聽聽聽聽聽el1.addContent(em);
聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽Element聽el2聽=聽new聽Element("鍏冪礌涓?).addContent("絎笁涓厓绱?);
聽聽聽聽聽聽聽聽聽聽聽聽聽root.addContent(el1);
聽聽聽聽聽聽聽聽聽聽聽聽聽root.addContent(el2);
聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽//緙╄繘鍥涗釜絀烘牸,鑷姩鎹㈣,gb2312緙栫爜
聽聽聽聽聽聽聽聽聽聽聽聽聽XMLOutputter聽outputter聽=聽new聽XMLOutputter("聽聽",聽true,"GB2312");
聽聽聽聽聽聽聽聽聽聽聽聽聽outputter.output(doc,聽new聽FileWriter("test.xml"));
聽聽聽聽聽聽聽聽聽}catch(Exception聽e)
聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽System.out.println(e);
聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽}
聽聽聽聽聽
聽聽聽聽聽public聽static聽void聽main(String聽args[])
聽聽聽聽聽{
聽聽聽聽聽聽new聽CreateXML().Create();
聽聽聽聽聽}
聽聽聽聽聽
聽}
聽2銆丏TD楠岃瘉鐨勶細
聽import聽java.io.*;
聽import聽org.jdom.*;
聽import聽org.jdom.input.*;
聽import聽org.jdom.output.*;
聽public聽class聽XMLWithDTD
聽{
聽聽public聽void聽validate()
聽聽{
聽聽聽try
聽聽聽{
聽聽聽聽SAXBuilder聽builder聽=聽new聽SAXBuilder(true);
聽聽聽聽builder.setFeature("http://xml.org/sax/features/validation";,true);
聽聽聽聽Document聽doc聽=聽builder.build(new聽FileReader("author.xml"));
聽聽聽聽
聽聽聽聽System.out.println("鎼炴巶");
聽聽聽聽XMLOutputter聽outputter聽=聽new聽XMLOutputter();
聽聽聽聽outputter.output(doc,聽System.out);
聽聽聽}catch(Exception聽e)
聽聽聽{
聽聽聽聽System.out.println(e);
聽聽聽}
聽聽聽
聽聽}
聽聽public聽static聽void聽main(String聽args[])
聽聽{
聽聽聽new聽XMLWithDTD().validate();
聽聽}
聽聽
聽}
聽
闇瑕佽鏄庣殑鏄紝榪欎釜紼嬪簭娌℃湁鎸囨槑浣跨敤鍝釜DTD鏂囦歡銆侱TD鏂囦歡鐨勪綅緗槸鍦╔ML涓寚瀹氱殑錛岃屼笖DTD涓嶆敮鎸佸懡鍚嶇┖闂達紝涓涓猉ML鍙兘寮曠敤涓涓狣TD錛屾墍
浠ョ▼搴忕洿鎺ヨ鍙朮ML涓寚瀹氱殑DTD錛岀▼搴忔湰韜笉鐢ㄦ寚瀹氥備笉榪囪繖鏍蜂竴鏉ワ紝濂借薄灝卞彧鑳戒嬌鐢ㄥ閮ㄥ紡鐨凞TD寮曠敤鏂瑰紡浜嗭紵楂樹漢鎸囩偣銆?/p>
聽3銆乆ML聽Schema楠岃瘉鐨勶細
聽import聽java.io.*;
聽import聽org.jdom.*;
聽import聽org.jdom.input.*;
聽import聽org.jdom.output.*;
聽public聽class聽XMLWithSchema
聽{
聽聽String聽xml="test.xml";
聽聽String聽schema="test-schema.xml";
聽聽public聽void聽validate()
聽聽{
聽聽聽try
聽聽聽{
聽聽聽聽SAXBuilder聽builder聽=聽new聽SAXBuilder(true);
聽聽聽聽//鎸囧畾綰︽潫鏂瑰紡涓篨ML聽schema
聽聽聽聽builder.setFeature("http://apache.org/xml/features/validation/schema";, 聽true);
聽聽聽聽//瀵煎叆schema鏂囦歡
聽聽聽聽builder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";,schema);
聽聽聽聽Document聽doc聽=聽builder.build(new聽FileReader(xml));
聽聽聽聽
聽聽聽聽System.out.println("鎼炴巶");
聽聽聽聽XMLOutputter聽outputter聽=聽new聽XMLOutputter();
聽聽聽聽outputter.output(doc,聽System.out);
聽聽聽}catch(Exception聽e)
聽聽聽{
聽聽聽聽System.out.println("楠岃瘉澶辮觸:"+e);
聽聽聽}
聽聽聽
聽聽}
聽聽
聽}
聽涓婇潰鐨勭▼搴忓氨鎸囧嚭浜嗚寮曞叆鐨刋ML聽Schema鏂囦歡鐨勪綅緗?/p>
聽緋葷粺榛樿杈撳嚭鏄疷TF-8錛岃繖鏈夊彲鑳藉鑷村嚭鐜頒貢鐮併?/p>
聽璇籜ML鏂囨。鐨勬妧宸у湪緗戜笂宸茬粡娉涙互錛岃繖閲屽氨娌℃湁鍐嶅垪鍑烘潵銆?/p>