8

Here is my xml file

<?xml version="1.0" encoding="UTF-8"?>
<productlist>
<product>
<title>1</title>
<price>30</price>
</product>
<product>
<title>2</title>
<price>9</price>
</product>
<product>
<title>3</title>
</product>
</productlist>

What I need to do is use Java DOM API to print out all the contents from XML file which contain "price" tag. Such things like this

title:1
price:30 
title:2 
price:9

In the org.w3c.dom documentation I only find hasAttribute(String name) method to check if this element have attribute but I can not find method like "hasTag(String name)" in documentation. I find this website http://www.java-forums.org/xml/62136-check-whether-xml-tag-exists-while-parsing-xml-using-dom-java.html but unfortunately I can not open the site. Hope you can help.

1 Answer 1

26

Just call getElementsByTagName and see if the returned list has any nodes (using NodeList.getLength()).

Sign up to request clarification or add additional context in comments.

1 Comment

seems this is the best way, little bit tricky anyway. But thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.