関連する読み物:
Ajaxを使用してファイルやその他のパラメーターをアップロードします(Java開発)
1。XMLファイル:
XMLとは何ですか? XMLは一般に、標準的な一般的なマークアップ言語のサブセットである拡張可能なマークアップ言語を指し、電子ファイルをマークして構造的にするために使用されるマークアップ言語です。
2。XMLファイルの利点:
1)XMLドキュメントの内容と構造は完全に分離されています。
2)強い相互運用性。
3)標準化および統合します。
4)複数のエンコーディングをサポートします。
5)強いスケーラビリティ。
3。XMLドキュメントを解析する方法:
異なる言語でのXMLドキュメントの解析は同じですが、実装構文は異なります。 2つの基本的な解析方法があります。 1つはSAXメソッドです。これは、XMLファイルの順に段階的に解析することです。別の分析方法はDOMメソッドであり、DOMメソッドの鍵はノードです。 Dom4J、JDOM、その他の方法もあります。この記事では、DOMおよびDOM4Jメソッドと、それらをカプセル化する方法をツールクラスにカプセル化して、XMLドキュメントを読み取る方法を紹介します。
4.xmlドキュメント:
scores.xml:
<?xml version = "1.0" encoding = "utf-8"?> <!doctype学生[<!要素学生(学生+)> <!要素学生(名前、コース、スコア)> < <NAME> ZHANG SAN </name> <Course> javase </course> <score> 100 </score> </sudine> <student id = "22"> <name> li si </name> <ouracle> oracle </course> <score> 98 </score> </sudint
5. XMLを解析します
public static void main(string [] args)は、parserconfigurationexception、saxexception、ioexception {// 1。 DOM Parser Factory DocumentBuilderFactory DBF = documentBuilderFactory.NewInstance()を作成します。 // 2。 Dom Parser Factory DocumentBuilder db = dbf.NewDocumentBuilder()からDOMパーサーを作成します。 // 3。 Dom Parserからドキュメントを解析して、DOMツリードキュメントdoc = db.parse( "scores.xml")を生成します。 // 4。 DOMツリーを解析し、ドキュメントコンテンツ(要素属性テキスト)を取得します//4.1ルート要素スコアnodeList scoresList = doc.getChildNodes()を取得します。ノードscoreSnode = scoreSlist.item(1); system.out.println(scoreslist.getLength()); //4.2スコアですべての子要素を取得した学生ノデリストStudentList = scoresNode.getChildNodes(); system.out.println(sustentlist.getLength()); //4.3各生徒のプロセス//system.out.println(stunode.getNodetype()); //出力要素の属性ID if(stunode.getNodetype()== node.element_node){element elem =(element)stunode; string id = elem.getattribute( "id"); System.out.println( "id ------>"+id); } //出力要素の子要素名コーススコアnodeList ncslist = stunode.getChildNodes(); //system.out.println(ncslist.getLength()); for(int j = 0; j <ncslist.getLength(); j ++){node ncs = ncslist.item(j); if(ncs.getnodetype()== node.element_node){string name = ncs.getNodename(); // string value = ncs.getFirstChild()。getNodevalue(); //テキストは要素の子です。 System.out.println(name+"----->"+value); }} system.out.println(); }}6。DOM4JメソッドXMLドキュメントの解析:
public static void main(string [] args)throws documentexception {// dom4jを使用してスコア2.xmlを解析し、dom tree saxreader reader = new Saxreader(); document doc = reader.read(new file( "scores.xml")); //ルートノードを取得:学生要素root = doc.getRootelement(); //学生のすべての子ノードを取得する:学生Iterator <lement> it = root.Elementiterator(); //各生徒をトレーニングしているwhile(it.hasnext()){//各学生要素を取得stuleem = it.next(); //system.out.println(stulem); //生徒の出力属性:IDリスト<属性> atturelist = stuelem.attributes(); for(attribute attr:attrlist){string name = attr.getName(); string value = attr.getValue(); System.out.println(name+"----->"+value); } //出力生徒の子要素:名前、コース、スコアIterator <lement> it2 = stuelem.elementiterator(); while(it2.hasnext()){element elem = it2.next();文字列名= elem.getName();文字列テキスト= elem.getText(); System.out.println(name+"----->"+text); } system.out.println(); }}もちろん、XMLをどのように解析しても、JARパッケージをインポートする必要があります(忘れないでください)。
7。私自身のやり方:
実際の開発プロジェクトでは、ツールクラスを使用して、使用する機能を繰り返しツールクラスにカプセル化する必要があります。したがって、次の方法は、開発プロセス中に使用する方法です。
7.1プロパティファイルとは:
7.1.1構造的に:
.xmlファイルは主にツリーファイルです。
.Propertiesファイルは、主にキー値のキー価値ペアの形式で存在します。
7.1.2柔軟な観点から:
.xmlファイルは、.propertiesファイルよりも柔軟です。
7.1.3便利な観点から:
.propertiesファイルは、.xmlファイルよりも構成が簡単です。
7.1.4アプリケーションの観点から:
.xmlはより柔軟であるため、プロパティファイルは小規模でシンプルなプロジェクトにより適しています。
7.2独自のプロパティドキュメント:
自分のプロジェクトでpath.porpertiesファイルを作成しました。これは、使用するパスを保存するために使用され、name = valueの形式で保存されます。例えば:
RealPath = D:/file/
7.3独自の.propertiesファイルを解析します:
public class propertiesutil {private static propertiesutil manager = null; private static object managlelock = new object(); private object propertieslock = new object(); private static string database_config_file = "/path.properties";私的プロパティプロパティ= null; public static propertiesutil getInstance(){if(manager == null){synchronized(managerlock){if(manager == null){manager = new PropertiesUtil(); }}} return Manager; } private propertiesutil(){} public static string getProperty(string name){return getInstance()._ getProperty(name); } private string _getProperty(string name){initproperty(); string property = properties.getProperty(name); if(property == null){return ""; } else {return property.trim(); }} public static enumeration <?> propertynames(){return getInstance()._ propertynames(); } private enumeration <? properties.propertynames()を返します。 } private void initproperty(){if(properties == null){synchronized(propertiesLock){if(properties == null){loadproperties(); }}}} private void loadproperties(){properties = new Properties(); inputstream in = null; try {in = getClass()。getResourceasStream(database_config_file); properties.load(in); } catch(Exception e){System.err .println( "PropertiesUtil.LoadProps()" + e)でconfプロパティを読み取るエラーe.printstacktrace(); }最後に{try {in.close(); } catch(Exception e){}}} / ** *構成ファイルパスを提供 * * @param filepath * @return * / public Properties loadproperties(string filepath){Properties Properties = new Properties(); inputstream in = null; try {in = getClass()。getResourceasStream(filepath); properties.load(in); } catch(Exception e){System.err .println( "PropertiesUtil.loadProperties()" + e)のconfプロパティを読み取ります。 e.printstacktrace(); }最後に{try {in.close(); } catch(Exception E){}} return Properties; }}使用する前に、.propertiesファイルの名前であるDATABASE_CONFIG_FILEプロパティに値を添付する必要があります。使用すると、クラス名を直接使用できます。 getProperty(“realPath”); RealPathである.Propertiesファイルのキーを使用してコンテンツを取得します。
上記は、編集者が紹介したJava開発のXMLおよびProperties Configurationファイルを読み取る方法です。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します!