2017-02-09 170 views
0

我想使用DOM解析下面的xml結構。使用DOM解析XML

<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> 

代碼如下:

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(); 

在這裏,如果我服用標籤「對」的所有第走到一起。但我想在分開的部分,如..

印度憲法是印度的最高法律。[1]它規定了確定基本政治原則的框架,確立了政府機構的結構,程序,權力和義務,並規定了基本權利,指導原則和公民義務。 引言 - 印度次大陸的主要部分是1857年至1947年在英國統治之下。當印度憲法於1950年1月26日生效時,它廢除了「印度獨立法」。印度不再是英國王室的統治地位,併成爲主權民主共和國。被選爲紀念1930年

我能爲在不同的標籤,但在一個共同的父標籤單獨服用標籤(與理智的名稱標籤)做獨立的普爾納斯瓦拉傑聲明1月26日的日期..

+0

使用SAX解析器,它將爲您提供xml的對象表示。 – Hashmap

+0

我想使用DOM – Kaira

+0

如果標籤都具有相同的名稱,您如何決定將內容置於不同的標籤? – DuKes0mE

回答

0

Xpath或CSS選擇器可能是更好的選擇。

+0

你能舉個例子嗎...... – Kaira