ļҪJavaԱиXMLĵֳ÷,ҷַӡ,ĻοJavaXMLĵĸʽչ
JAXPJava API for XML ProcessingӢͷд,ĺ:XMLĵʹJavaԱдı̽ӿڡJAXP֧DOMSAXXSLTȱΪǿJAXPʹϵ,رΪJAXPһPluggability Layer,Pluggability Layer֧֮,JAXPȿԺ;ʵDOM APISAX API ĸXML(XML Parser,Apache Xerces)Ϲ,ֿԺ;ִXSLTXSLT(XSLT Processor,Apache Xalan)ϹӦPluggability Layerĺô:ֻҪϤJAXP̽ӿڵĶ弴,ҪõľXMLXSLTк˽⡣ijJava,ͨJAXPXMLApache CrimsonXMLĵд,ϣʹñXML(Apache Xerces),Ա߸ó,ôԭܲҪκθı,ֱӾͿʹ(ҪֻǽApache Xercesjarļ뵽CLASSPATH,Apache CrimsonjarļڻCLASSPATHɾ)
ĿǰJAXPѾӦõʮձ,˵JavaдXMLĵıAPIЩѧѧϰʹJAXPĹ,:ұдijDOM Tree˸,ǵ˳Ժ,ԭʼXMLĵûиı,,ʵֶԭʼXMLĵDOM Treeͬ?զһ,JAXPƺûṩӦĽӿ//,Ǻܶѧ߶е⡣ĵּڽ,ĽֳܼõͬԭʼXMLĵDOM TreeķΪС۵ķΧ,漰XMLApache CrimsonApache Xerces,XSLTʹApache Xalan
һ:ֱӶдXMLĵ
Ҳԭʼİ취ˡȡDOM Tree֮,ӦDOMģ͵NodeӿڵĸDOM Treeи,һӦöԭʼXMLĵиˡǿõݹİ취ӦTreeWalker,DOM Tree,ͬʱ,DOM Treeÿһڵ/Ԫд뵽ԤȴԭʼXMLĵ,DOM Treeȫ֮,DOM TreeԭʼXMLĵʵͬ¡ʵ,ʹ,ҪʵԼXML,ַпõϵġ
:ʹXmlDocument
ʹXmlDocument?JAXPзûѽ!Dz߸?ûд!ʹXmlDocument,ȷе˵,ʹXmlDocumentwrite()
Ѿᵽ,JAXPԺָXMLʹ,ѡõXMLApache CrimsonXmlDocument(org.apache.crimson.tree.XmlDocument)Apache Crimsonһ,ڱJAXP,ѹJAXPĵҲXmlDocumentķء,ӦXmlDocumentʵָXMLĵĹ?XmlDocumentṩwrite()(Crimsonµİ汾------Apache Crimson 1.1.3):
public void write (OutputStream out) throws IOException public void write (Writer out) throws IOException public void write (Writer out, String encoding) throws IOException
write()ҪþDOM Treeеݵض,ļӦǫ́ȵȡôʹwrite()?뿴JavaƬ:
String name="fancy"; DocumentBuilder parser; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { parser = factory.newDocumentBuilder(); Document doc = parser.parse("user.xml"); Element newlink=doc.createElement(name); doc.getDocumentElement().appendChild(newlink); ((XmlDocument)doc).write(new FileOutputStream(new File("xuser1.xml"))); } catch (Exception e) { //to log it }
Ĵ,ȴһDocumentdoc,ȡDOM Tree,ȻӦNodeӿڵappendChild(),DOM Treeһ½ڵ(fancy),XmlDocumentwrite(OutputStream out),DOM Treeеxuser.xml(ʵҲuser.xml,ԭʼXMLĵ,Ϊ˱Ա,ʶxuser.xmlļ)ҪעDzֱӶDocumentdocֱӵwrite(),ΪJAXPDocumentӿڲûжκwrite(),Ա뽫docDocumentǿתΪXmlDocument,Ȼܵwrite(),Ĵʹõwrite(OutputStream out),ʹȱʡUTF-8DOM Treeеݵض,DOM Treeаַ,ôĽп,༴ν"",İ취ʹwrite (Writer out, String encoding),ʽָʱı,罫ڶΪ"GB2312",ʱ"",ܹʾַ
οļ: AddRecord.java()user.xml()ӵлΪ:Windows XP ProfessionalJDK 1.3.1ΪܹAddRecord.java,Ҫַhttp://xml.apache.org/dist/crimson/ȥApache Crimson,ȡcrimson.jarļ뵽CLASSPATHС
ע:
Apache CrimsonǰSun Project X Parser,֪ι,X ParserݱΪApache Crimson,Apache Crimsonĺܶ붼ǴX ParserֱֲġõXmlDocument,X Parsercom.sun.xml.XmlDocument,Apache Crimsonҡһ,ͱorg.apache.crimson.tree.XmlDocument,ʵǵľִһ,ܾpackageimportԼļͷһlienceͬѡڵJAXPǺX Parserһ,һЩϵijʹcom.sun.xml,±,пܲͨ,϶ΪԭJAXPApache Crimsonһ,JAXP 1.1,ʹJAXP 1.1,ôҪApache Crimson,Ҳܹ(AddRecord.java)µJAXP 1.2 EA(Early Access)Ҹ,ܸõApache XalanApache XercesֱΪXSLTXML,ֱ֧Apache Crimson,ĿJAXP 1.2 EAJava XML Pack(ںJAXP 1.2 EA),ôֱӱ(AddRecord.java),ҪزװApache Crimson
:ʹTransformerFactoryTransformer
JAXPṩıĸԭʼXMLĵķǵXSLT,༴ʹTransformerFactoryTransformerࡣ뿴JavaƬ:
//ȴһDOMSource,ù캯IJһDocument //docĺDOM Tree DOMSource doms = new DOMSource (doc);
//һFile,DOM Treeݵ,һXMLļ File f = new File ("XMLOutput.xml");
//һStreamResult,ù캯IJȡΪFile StreamResult sr = new StreamResult (f);
//JAXPеXSLTʵDOM TreeеݵXMLļеĹܡ //XSLTΪDOMSource,ΪStreamResut try { //ȴһTransformerFactory,ɴ˴TransformerTransformer //൱һXSLT档ͨʹXSLļ,ʹ //XMLĵ TransformerFactory tf=TransformerFactory.newInstance(); Transformer t=tf.newTransformer ();
//ؼһ, Transformer (XSLT)transform(),÷ĵһ //DOMSource,ڶStreamResult t.transform(doms,sr); } catch (TransformerConfigurationException tce) { System.out.println("Transformer Configuration Exception\n-----"); tce.printStackTrace(); } catch (TransformerException te) { System.out.println ("Transformer Exception\n---------"); te.printStackTrace (); }
ʵʵӦ,ǿӦôͳDOM APIXMLĵлȡDOM Tree,ȻʵʵDOM Treeִиֲ,õյDocument,ɴDocumentDOMSource,ʣµհĴ,Ϻ, XMLOutput.xmlҪĽ(Ȼ,StreamResult캯IJ,ָͬ,ǧƪһɵXMLĵ)
ĺôڿĿDOM Treeееĸʽ,ǹTransformerFactoryTransformerಢʵ,ҪOutputKeysİ οļ: AddRecord2.java()user.xml()ӵлΪ:Windows XP ProfessionalJDK 1.3.1ΪܹAddRecord2.java,Ҫַhttp://java.sun.comȥذװJAXP 1.1Java XML Pack(Java XML PackѾںJAXP)
OutputKeys
javax.xml.transform.OutputKeysjava.util.Propertiesʹ,ԿJAXPXSLT(Transformer)XMLĵĸʽ뿴ĴƬ:
//ȴһTransformerFactory,ɴ˴Transformer TransformerFactory tf=TransformerFactory.newInstance(); Transformer t=tf.newTransformer ();
//ȡTransformser,༴XSLTȱʡ,һ //java.util.Properties Properties properties = t.getOutputProperties();
//µ:ַΪGB2312,ַ֧,XSLT //XMLĵַ,ʾ,ν"" //OutputKeysַOutputKeys.ENCODING properties.setProperty(OutputKeys.ENCODING,"GB2312");
/XSLTԡ t.setOutputProperties(properties);
//XSLT,е,DOM TreeеݵС t.transform(DOMSource_Object,StreamResult_Object);
ij,Dzѿ,ͨXSLT(Transformer),ԿDOM Treeеݵʽ,ǶǺаġôJAXPXSLT(Transformer)ЩԿ? javax.xml.transform.OutputKeysඨ˺ַܶ,Ƕǿõ,õʾ:
public static final java.lang.String METHOD
Ϊ"xml""html""text"ֵ
public static final java.lang.String VERSION
ѭ淶İ汾,METHODΪ"xml",ôֵӦΪ"1.0",METHODΪ"html",ôֵӦΪ"4.0",METHODΪ"text",ôԻᱻԡ
public static final java.lang.String ENCODING
ʱõı뷽ʽ,"GB2312""UTF-8"ȵ,Ϊ"GB2312",Խν""
public static final java.lang.String OMIT_XML_DECLARATION
XMLĵʱǷXML,༴:
Ĵ롣ѡֵ"yes""no"
public static final java.lang.String INDENT
IDENT趨XSLTXMLĵʱ,ǷԶӶĿո,ѡֵΪ"yes""no"
public static final java.lang.String MEDIA_TYPE
MEDIA_TYPE趨ĵMIME͡
趨XSLT?ܽһ:
ǻȡXSLT(Transformer)ȱʡԵļ,ҪʹTransformergetOutputProperties(),ֵһjava.util.Properties
Properties properties = transformer.getOutputProperties();
Ȼ趨µ,:
properties.setProperty(OutputKeys.ENCODING,"GB2312"); properties.setProperty(OutputKeys.METHOD,"html"); properties.setProperty(OutputKeys.VERSION,"4.0");
ǸXSLT(Transformer)ȱʡԵļ,ҪʹTransformersetOutputProperties(),һjava.util.Properties
DZдһµij,ӦOutputKeys,ԿXSLT,óļܹǰһ(AddRecord3.java)ͬ,вͬĴοļ: AddRecord3.java()user.xml()ӵлΪ:Windows XP ProfessionalJDK 1.3.1ΪܹAddRecord3.java,Ҫַhttp://java.sun.comȥذװJAXP 1.1Java XML Pack(Java XML PackںJAXP)
:ʹXalan XML Serializer
ʵǷһ,ҪApache XalanApache Xercesֲ֧ܹСӴʾ:
//ȴһDOMSource,ù캯IJһDocument //docĺDOM Tree DOMSource domSource = new DOMSource (doc);
//һDOMResult,ʱXSLT DOMResult domResult = new DOMResult();
//JAXPеXSLTʵDOM TreeеݵXMLļеĹܡ //XSLTΪDOMSource,ΪDOMResut try { //ȴһTransformerFactory,ɴ˴TransformerTransformer //൱һXSLT档ͨʹXSLļ,ʹ //XMLĵ TransformerFactory tf=TransformerFactory.newInstance(); Transformer t=tf.newTransformer ();
//XSLT(ز,"") Properties properties = t.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING,"GB2312"); t.setOutputProperties(properties);
//ؼһ, Transformer (XSLT)transform(),÷ĵһ //DOMSource,ڶDOMResult t.transform(domSource,domResult);
//ȱʡXalan XML Serializer,ʹʱDOMResult //(domResult)еʽС Serializer serializer = SerializerFactory.getSerializer (OutputProperties.getDefaultMethodProperties("xml"));
//Xalan XML Serializer,һز,Ҳܲ //ν"" Properties prop=serializer.getOutputFormat(); prop.setProperty("encoding","GB2312"); serializer.setOutputFormat(prop);
//һFile,DOM Treeݵ,һXMLļ File f = new File ("xuser3.xml");
//ļfos,캯IJ FileOutputStream fos=new FileOutputStream(f);
//Xalan XML Serializer serializer.setOutputStream(fos);
//л serializer.asDOMSerializer().serialize(domResult.getNode()); } catch (Exception tce) { tce.printStackTrace(); }
̫,ƺе㻭,ǾͲչˡοļ: AddRecord4.java()user.xml()ӵлΪ:Windows XP ProfessionalJDK 1.3.1ΪܹAddRecord4.java,Ҫַhttp://xml.apache.org/dist/ȥذװApache XalanApache Xerces
ǵַhttp://java.sun.com/xml/download.htmlȥذװJava XML PackΪµJava XML Pack(Winter 01 )Apache XalanApache Xercesڡ
:
ļԵJavaԱиXMLĵַһֱַӶдXMLļ,ַʮַ,ұȽ׳,ʹ,ҪԼXML Parser,ʹַڶַʹApache CrimsonXmlDocument,ַΪ,ʹ÷,ѡApache CrimsonΪXML,ôʹַ,ַƺЧʲ(ԴЧʵµApache Crimson),,߰汾JAXPJava XML PackJWSDPֱ֧Apache Crimson,༴ַͨáַʹJAXPXSLT(Transformer)XMLĵ,ַҲDZķ,ʹʮ,رǿʽ,Ƽַַǵַı,Xalan XML Serializer,˴л,ڴĵ/Խ,ϧҪظXSLTԺXML Serializer,Ƚ鷳,Apache XalanApache Xerces,ͨԲ㡣
۵ַ,ʵӦñAPI(JDOMCastorXML4JOracle XML Parser V2)Ҳкܶ취ԸXMLĵ,ƪ,Ͳһһˡ
οԼԴ:
[1]The Java Web Services Tutorial, Sun Microsystems Inc.
[2]http://xml.apache.org,Apache XML Project(CrimsonXercesXalan)
[3]http://www.jguru.com,XML Forum
[4]http://forum.java.sun.com,Java Technology & XML Forum
|