Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read alternative tags in xml using java?
    primarykey
    data
    text
    <p>I have xml file</p> <pre><code>&lt;A&gt; &lt;A1&gt; &lt;A2&gt;Hi&lt;/A2&gt; &lt;/A1&gt; &lt;A&gt; &lt;B&gt; &lt;B1&gt;&lt;/B1&gt; &lt;B2&gt;100&lt;/B2&gt; &lt;/B&gt; &lt;A&gt; &lt;A1&gt; &lt;A2&gt;Hello&lt;/A2&gt; &lt;/A1&gt; &lt;A&gt; &lt;B&gt; &lt;B1&gt;1000&lt;/B1&gt; &lt;B2&gt;&lt;/B2&gt; &lt;/B&gt; </code></pre> <p>likewise this goes more than 10 blocks. Now my java code able to read one by one that is first reads all after that reads tag.</p> <p>Code:</p> <pre><code>public class XMLParse { static Document doc; public static void main(String argv[]) { try { File file = new File("/home/dev042/Desktop/xxx.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(file); doc.getDocumentElement().normalize(); System.out.println("Root element " + doc.getDocumentElement().getNodeName()); NodeList nodeLst = doc.getElementsByTagName("A"); System.out.println("Information of all Balence Sheet"); int count = nodeLst.getLength(); String name; for (int s = 0; s &lt; nodeLst.getLength(); s++) { Node fstNode = nodeLst.item(s); if (fstNode.getNodeType() == Node.ELEMENT_NODE) { Element fstElmnt = (Element) fstNode; NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("A1"); for(int i =0; i &lt; fstNmElmntLst.getLength(); i++ ) { Node lst = fstNmElmntLst.item(i); if(lst.getNodeType() == Node.ELEMENT_NODE) { Element fsttravel = (Element) lst; NodeList secNmElt = fsttravel.getElementsByTagName("*"); name = secNmElt.item(0).getTextContent(); System.out.println("Name : " + name); } } } } } catch (Exception e) { e.printStackTrace(); } String amt; double amount; NodeList nodeLst = doc.getElementsByTagName("B"); int coun = nodeLst.getLength(); for (int s = 0; s &lt; nodeLst.getLength(); s++) { Node secNode = nodeLst.item(s); if (secNode.getNodeType() == Node.ELEMENT_NODE) { try { Element amtval = (Element) secNode; NodeList secval = amtval.getElementsByTagName("B1"); amt = secval.item(0).getTextContent(); //amount = Double.parseDouble(amt); System.out.println("SubAmt :" + amt); NodeList lstNmElmntLst = amtval.getElementsByTagName("B2"); amt = lstNmElmntLst.item(0).getTextContent(); System.out.println("MainAmt : " +amt); } catch(Exception ex){ ex.printStackTrace(); } } } } } </code></pre> <p>current output:</p> <pre><code>Hi Hello 100 1000 </code></pre> <p>I want to read the tags alternatively. then only i can able map the values. How can i read these tags alternatively. output should be like this</p> <pre><code>Hi 100 Hello 1000 </code></pre> <p>Kindly help me out of it. Thanks in advance..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload