troubleshooting Question

Java DOM dealing with the same tag names

Avatar of sdittmann
sdittmann asked on
Java
8 Comments2 Solutions440 ViewsLast Modified:
Hello,

I'm reading in a xml file into a java program and accessing the nodes by getElementsByTagName.  I'm running into an issue where there are duplicate tag names of <BEG> and <END> and I'm picking up the wrong information along the way.
This is apparent under the tags RATES/RS/BEG which have issues with OPS/FROM/TO/BEG.


I've tried xpath but that seems to read all the nodes in the document that I point to.  I was unable to successfully populate the java getters and setters through this method.

Any ideas on how I can loop through the 70 AVL nodes and populate my java class with all the details and deal with the duplicate tag names?

avl av = new avl();//Java class with getters and setters

NodeList avl = doc.getElementsByTagName("AVL");


NodeList nl = ele.getElementsByTagName(tagName);


String type = getTextValue(elementetd, "TYPE");
                        av.settype(type);
                  
String ospid = getTextValue(elementetd, "OSPID");
av.setOspid(ospid);
                  
            
String name = getTextValue(elementetd, "NAME");
av.setName(name);
                        
String desc = getTextValue(elementetd, "DESC");
av.setDesc(desc);
                        
String inter = getTextValue(elementetd, "INTER");
av.setInter(inter);
            
                        
String tel = getTextValue(elementetd, "TEL");
av.setTel(tel);
                        

String rates = getEstRates(elementetd, "RATE",av);  //I get bad data in here


private String getTextValue(Element ele, String tagName) {
            String textVal = null;
            NodeList nl = ele.getElementsByTagName(tagName);
            if (nl != null && nl.getLength() > 0) {
                  Element el = (Element) nl.item(0);
                  textVal = el.getFirstChild().getNodeValue();
            }

            return textVal;
      }


private String getEstRates(Element ele, String tagName, avl dt) {
            String textVal = null;
            
            ArrayList<Rates> alRates = new ArrayList<Rates>(6);
      
            NodeList nl = ele.getElementsByTagName(tagName);
            
            if (nl != null && nl.getLength() > 0) {
                  for (int i = 0; i < nl.getLength(); i++) {
                        Rates rts = new Rates();
                        Element el = (Element) nl.item(i);
                        textVal = el.getFirstChild().getNodeValue();
                        
                        rts.setRate(textVal);
                        
                        
                        
                        NodeList nlTo = ele.getElementsByTagName("BEG");
                        if (nlTo != null && nlTo.getLength() > 0){
                              
                                    Element elTo = (Element) nlTo.item(i);
                                    if(elTo != null){
                                                                  
                                          textVal = elTo.getFirstChild().getNodeValue();
                                          rts.setBeg(textVal);
                              
                                    }      
                                          
                        
                        }      
                                    alRates.add(rts);
                                    dt.setAlRates(alRates);
                        
                                    
                  } //End of Outter most for loop
                  
            
            }
            
            return textVal;
      }
      
      



There are approximately 70 AVL nodes.  2 are listed below:
<AVL>
<TYPE>OFF</TYPE>
<OSPID>934</OSPID>
<NAME>GG Garage</NAME>
<DESC>250 Clay Street</DESC>
<INTER>Clay between Front & Davis</INTER>
<TEL>(415) 433-4722</TEL>
<OPHRS>
<OPS>
<FROM>Monday</FROM>
<TO>Friday</TO>
<BEG>4:00 AM</BEG>
<END>10:00 PM</END>
</OPS>
<OPS>
<FROM>Saturday</FROM>
<BEG>7:00 AM</BEG>
<END>10:00 PM</END>
</OPS>
<OPS>
<FROM>Sunday</FROM>
<BEG>9:00 AM</BEG>
<END>10:00 PM</END>
</OPS>
</OPHRS>
<RATES>
<RS>
<DESC>Incremental</DESC>
<RATE>7</RATE>
<RQ>Per hour</RQ>
</RS>
<RS>
<DESC>24-Hour Max/Lost Tkt</DESC>
<RATE>36</RATE>
<RQ>Flat rate</RQ>
</RS>
<RS>
<DESC>Early Bird</DESC>
<RATE>20</RATE>
<RQ>Flat rate</RQ>
<RR>Mon-Fri: In by 10AM/Out by 7PM</RR>
</RS>
<RS>...</RS>
<RS>...</RS>
<RS>...</RS>
<RS>...</RS>
</RATES>
<OCC>828</OCC>
</AVL>
<AVL>...</AVL>
<AVL>
<TYPE>ON</TYPE>
<BFID>380002</BFID>
<NAME>Davis St</NAME>
<RATES>
<RS>
<BEG>12:00 AM</BEG>
<END>2:00 AM</END>
<RATE>0</RATE>
<RQ>No charge</RQ>
</RS>
<RS>
<BEG>2:00 AM</BEG>
<END>6:00 AM</END>
<RATE>0</RATE>
<RQ>Str sweep</RQ>
</RS>
<RS>
<BEG>6:00 AM</BEG>
<END>7:00 AM</END>
<RATE>0</RATE>
<RQ>No charge</RQ>
</RS>
<RS>
<BEG>7:00 AM</BEG>
<END>3:00 PM</END>
<RATE>0</RATE>
<RQ>Restricted</RQ>
</RS>
<RS>
<BEG>3:00 PM</BEG>
<END>6:00 PM</END>
<RATE>0</RATE>
<RQ>Tow away</RQ>
</RS>
<RS>
<BEG>6:00 PM</BEG>
<END>12:00 AM</END>
<RATE>0</RATE>
<RQ>No charge</RQ>
</RS>
</RATES>
<OCC>0</OCC>
</AVL>



Thanks in advance.

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros