close nav

close nav

Thread: XML Parsing issue

Forum : XML and XML Programming.   26 Jun, 2021
User Image
SARVESH

Newbie

Joined: 18 Dec, 2020

Points: 100

Threads: 1

Posts: 0

XML Parsing issue

| Quote Date : 26 Jun, 2021    Views:228    

Could you please suggest on below code where i am doing mistakes. I am getting StatusMessage as printed for 12 times but i want only 6. each filing tag contains 3 status message.

package com.sarvesh.search;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XmlParser {
static String str ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
 
"<response>" +
"   <fipMode>false</fipMode>" +
 
"   <filing>" +
 
"       <clientFilingId>45-92-159-182-67-157-32-187-153-190-122-14-145-194-33-171_0_1614297423677</clientFilingId>" +
 
"       <filingType>IRS-IIT-FILING</filingType>" +
 
"       <rxTimestamp>2021-02-25T15:57:03.691-08:00</rxTimestamp>" +
 
"       <postmark>2021-02-25T15:57:03.691-08:00</postmark>" +
 
"       <confirmationNumber>123456202105600199kp</confirmationNumber>" +
 
"       <statusMessage>" +
 
"           <timestamp>2021-02-25T15:57:03.692-08:00</timestamp>" +
 
"           <filingState>PENDING_EFE</filingState>" +
 
"           <message>Your return is being processed. Expect an acknowledgement within 24-48 hours.</message>" +
 
"       </statusMessage>" +
 
"       <statusMessage>" +
 
"           <timestamp>2021-02-25T15:57:40.977-08:00</timestamp>" +
 
"           <filingState>PENDING_AGENCY</filingState>" +
 
"           <message>Filing changed state to IN_AGENCY</message>" +
 
"       </statusMessage>" +
 
"       <statusMessage>" +
 
"           <timestamp>2021-02-25T15:58:01.226-08:00</timestamp>" +
 
"           <filingState>ACK_SUCCEEDED</filingState>" +
 
"           <message>Filing changed state to SUCCEEDED_AGENCY</message>" +
 
"       </statusMessage>" +
 
"   </filing>" +
 
"   <filing>" +
 
"       <clientFilingId>45-92-159-182-67-157-32-187-153-190-122-14-145-194-33-171_0_1614297424619</clientFilingId>" +
 
"       <filingType>CA-IIT-FILING</filingType>" +
 
"       <rxTimestamp>2021-02-25T15:57:04.660-08:00</rxTimestamp>" +
 
"       <postmark>2021-02-25T15:57:04.660-08:00</postmark>" +
 
"       <confirmationNumber>123456202105600199kr</confirmationNumber>" +
 
"       <statusMessage>" +
 
"           <timestamp>2021-02-25T15:57:04.664-08:00</timestamp>" +
 
"           <filingState>PENDING_EFE</filingState>" +
 
"           <message>Your return has been received and will be forwarded to the taxing agency at the appropriate time.</message>" +
 
"       </statusMessage>" +
 
"       <statusMessage>" +
 
"           <timestamp>2021-02-25T15:58:21.057-08:00</timestamp>" +
 
"           <filingState>PENDING_AGENCY</filingState>" +
 
"           <message>Filing changed state to IN_AGENCY</message>" +
 
"       </statusMessage>" +
 
"       <statusMessage>" +
 
"           <timestamp>2021-02-25T15:58:40.817-08:00</timestamp>" +
 
"           <filingState>ACK_SUCCEEDED</filingState>" +
 
"           <message>Filing changed state to SUCCEEDED_AGENCY</message>" +
 
"       </statusMessage>" +
 
"   </filing>" +
 
"</response>";
public static void main(String[] args) {
DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
bf.setIgnoringElementContentWhitespace(true);
try {
//Document db = bf.newDocumentBuilder().parse(new File(".\\src\\com\\sarvesh\\search\\efe.xml"));
Document db = bf.newDocumentBuilder().parse(new InputSource(new StringReader(str)));
Element root = db.getDocumentElement();
System.out.println(root.getNodeName());
NodeList nList = db.getElementsByTagName("response");
System.out.println(nList.getLength());
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node node = nList.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
    System.out.println("fipMode : " + eElement.getElementsByTagName("fipMode").item(0).getTextContent());
    System.out.println("Filing : "  + eElement.getElementsByTagName("filing").getLength());
    NodeList filingNode = eElement.getElementsByTagName("filing");
    for(int tem = 0; tem < filingNode.getLength(); tem++) {
    System.out.println("clientFilingId : " + eElement.getElementsByTagName("clientFilingId").item(tem).getTextContent());
    System.out.println("filingType : " + eElement.getElementsByTagName("filingType").item(tem).getTextContent());
    System.out.println("rxTimestamp : " + eElement.getElementsByTagName("rxTimestamp").item(tem).getTextContent());
    System.out.println("postmark : " + eElement.getElementsByTagName("postmark").item(tem).getTextContent());
    System.out.println("confirmationNumber : " + eElement.getElementsByTagName("confirmationNumber").item(tem).getTextContent());
    System.out.println("statusMessage : " + eElement.getElementsByTagName("statusMessage").getLength());
    NodeList statusNode = eElement.getElementsByTagName("statusMessage");
    for(int st=0;st<statusNode.getLength();st++) {
    System.out.println("timestamp : " + eElement.getElementsByTagName("timestamp").item(st).getTextContent());
    System.out.println("filingState : " + eElement.getElementsByTagName("filingState").item(st).getTextContent());
    System.out.println("message : " + eElement.getElementsByTagName("message").item(st).getTextContent());
    }
    }
}
}
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Load More

No More Comments