V
V
Vladimir2019-03-08 22:14:26
Java
Vladimir, 2019-03-08 22:14:26

How to count the number of external tags only?

Inside object there are more object tags ...
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("D:\\doc.xml");

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_201" class="java.beans.XMLDecoder">
 
<object class="com.labs.crud.classes.Order">
  <void property="id">
   <int>1</int>
  </void>
  <object>
  ...
  </object>
</object>
 
<object class="com.labs.crud.classes.Order">
  <void property="id">
   <int>2</int>
  </void>
  <object>
  ...
  </object>
</object>

</java>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@pestunov, 2019-03-10
_

I propose to take the child nodes of the "java" tag and select those with name = "object".

NodeList nodes = doc.getElementsByTagName("java").item(0)
        .getChildNodes();

    for (int i = 0; i < nodes.getLength(); i++) {
      if (nodes.item(i).getNodeName().equals("object")) {
        System.out.println(nodes.item(i).getNodeName());
      }

    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question