0
我想只得到餐館在貝魯特與DOM解析器,請幫助DOM解析器getElement
這是從我的XML文件的一部分:
城市 - > beirut->餐館,>餐廳 - >名稱 - >
tyr->restaurants->restaurant->name->
jbeil->restaurants->restaurant->name->
<restaurants>
<restaurant>
<name>
...
</restaurant>
</restaurants>
<restaurants>
<restaurant>
<name>
...
</restaurant>
</restaurants>
<restaurants>
<restaurant>
<name>
...
</restaurant>
</restaurants>
因爲這段代碼給我所有餐廳所有城市:
嘗試{
File inputFile = new File("src/josephXml.xml");
DocumentBuilderFactory dbFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Restaurant");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
jTextArea1.append("\n"+"Name : "+ eElement
.getElementsByTagName("name")
.item(0)
.getTextContent()+"\n "
,我只希望餐廳貝魯特請幫