2017-02-09 181 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