2017-02-09 5 views
0

DOMを使用して次のXML構造を解析します。私はすべてのパラグラフが一緒に来ているタグ「パラ」を取っていた場合DOMを使用してXMLを解析する

ここ
Element bodyElem = doc.getElementsByTagName("body").item(0); 
Element secElem = bodyElem.getElementsByTagName("sec").item(0); 
String title = secElem.getElementsByTagName("title").item(0).getTextContent(); 
String para = secElem.getElementsByTagName("para").item(0).getTextContent(); 

<body> 
<para> The Constitution of India is the supreme law of India.[1] It lays down the framework defining fundamental political principles, establishes the structure, procedures, powers and duties of government institutions and sets out fundamental rights, directive principles and the duties of citizens.</para> 
<sec> 
<title>INTRODUCTION</title> 
<para>The major portion of the Indian subcontinent was under British rule from 1857 to 1947. When the Constitution of India came into force on 26 January 1950, it repealed the Indian Independence Act. India ceased to be a dominion of the British Crown and became a sovereign democratic republic. The date of 26 January was chosen to commemorate the Purna Swaraj declaration of independence of 1930.</para> 
</sec> 
</body> 

コードは次のようになります。しかし、私は別のセクションで..のようにしたい。

インドの憲法はインドの最高法律である[1]それは、基本的な政治的原則を規定する枠組みを定め、政府機関の構造、手続き、権限と義務を確立し、基本的な権利、指令原則、市民の義務を定めている。 はじめに - インド亜大陸の大部分は、1857年から1947年までイギリス統治下にあった。インドの憲法が1950年1月26日に発効したとき、それはインド独立法を廃止した。インドは英国の王冠の支配をやめ、主権民主共和国となった。 1月26日の日付は、私は別のタグの下ではなく、共通の親タグで別々に(まともな名前のタグ)のタグを取るために何ができる1930年

の独立性のプールナスワラジ宣言を記念するために選ばれました。..

+0

xmlのオブジェクト表現を提供するSAX-Parserを使用します。 – Hashmap

+0

DOMを使用したいと思った – Kaira

+0

タグがすべて同じ名前の場合、どのようにコンテンツを別のタグに入れるのですか? – DuKes0mE

答えて

0

XpathやCSSセレクターが良い選択かもしれません。

+0

例ができますか? – Kaira

関連する問題