Question : Find siblings and parent nodes in DOM

Hi, having this code, which find all the nodes Series , i would like to print whats in this element Series, and also print another sibling node which shos the  Start element that will tell me the time the series start at a different channel...

=============================TVGuide.xml================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<TVGuide start="2001-07-05" end="2001-07-05">
      <Channel>
            <Name>BBC1</Name>
            <Program>
                  <Start>2001-07-05T19:00:00</Start>
                  <Duration>PT30M</Duration>
                  <Series>QuestionOfSport</Series>
                  <Title/>
            </Program>
            <Program rating="5" flag="favorite">
                  <Start>2001-07-05T19:30:00</Start>
                  <Duration>PT30M</Duration>
                  <Series>EastEnders</Series>
                  <Title/>
                  <Description>Mark's health scare forces him to reconsider his future with Lisa, while Jamie is torn between Sonia and Zoe.</Description>
                  <CastList>
                        <CastMember>
                              <Character>
                                    <Name>Zoe Slater</Name>
                              </Character>
                              <Actor>
                                    <Name>Michelle Ryan</Name>
                              </Actor>
                        </CastMember>
                        <CastMember>
                              <Character>
                                    <Name>Jamie Mitchell</Name>
                              </Character>
                              <Actor>
                                    <Name>Jack Ryder</Name>
                              </Actor>
                        </CastMember>
                        <CastMember>
                              <Character>
                                    <Name>Sonia Jackson</Name>
                              </Character>
                              <Actor>
                                    <Name>Natalie Cassidy</Name>
                              </Actor>
                        </CastMember>
                  </CastList>
                  <Writers>
                        <Writer>
                              <Name>Nick Saltrese</Name>
                        </Writer>
                        <Writer>
                              <Name>Julie Wassmer</Name>
                        </Writer>
                  </Writers>
                  <Director>
                        <Name>Stewart Edwards</Name>
                  </Director>
                  <Producer>
                        <Name>Emma Turner</Name>
                  </Producer>
            </Program>
            <Program type="documentary">
                  <Start>2001-07-05T20:00:00</Start>
                  <Duration>PT45M</Duration>
                  <Series/>
                  <Title>Get Real with Casualty</Title>
            </Program>
            <Program>
                  <Start>2001-07-05T20:45:00</Start>
                  <Duration>PT45M</Duration>
                  <Series>Lottery</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T21:00:00</Start>
                  <Duration>PT1H</Duration>
                  <Series>Panorama</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T22:00:00</Start>
                  <Duration>PT45M</Duration>
                  <Series>Lottery</Series>
                  <Title/>
            </Program>
      </Channel>
      <Channel>
            <Name>BBC2</Name>
            <Program>
                  <Start>2001-07-05T19:00:00</Start>
                  <Duration>PT1H</Duration>
                  <Series>Snooker</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T20:00:00</Start>
                  <Duration>PT1H</Duration>
                  <Series>HomeFront</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T21:00:00</Start>
                  <Duration>PT50M</Duration>
                  <Series>WildAfrica</Series>
                  <Title/>
            </Program>
            <Program flag="interesting">
                  <Start>2001-07-05T21:50:00</Start>
                  <Duration>PT40M</Duration>
                  <Series>Taboo</Series>
                  <Title>Nakedness</Title>
            </Program>
            <Program>
                  <Start>2001-07-05T22:30:00</Start>
                  <Duration>PT45M</Duration>
                  <Series>Lottery</Series>
                  <Title/>
            </Program>
      </Channel>
      <Channel>
            <Name>ITV</Name>
            <Program>
                  <Start>2001-07-05T19:00:00</Start>
                  <Duration>PT30M</Duration>
                  <Series>Emmerdale</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T19:30:00</Start>
                  <Duration>PT30M</Duration>
                  <Series>CoronationStreet</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T20:00:00</Start>
                  <Duration>PT1H</Duration>
                  <Series>Millionaire</Series>
                  <Title/>
            </Program>
            <Program type="drama">
                  <Start>2001-07-05T21:00:00</Start>
                  <Duration>PT2H</Duration>
                  <Series/>
                  <Title>Hot Money</Title>
            </Program>
      </Channel>
      <Channel>
            <Name>Channel 4</Name>
            <Program>
                  <Start>2001-07-05T19:00:00</Start>
                  <Duration>PT55M</Duration>
                  <Series>Channel4News</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T19:55:00</Start>
                  <Duration>PT5M</Duration>
                  <Series>SlotArt</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T20:00:00</Start>
                  <Duration>PT30M</Duration>
                  <Series>Brookside</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T20:30:00</Start>
                  <Duration>PT30M</Duration>
                  <Series>Brookside</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T21:00:00</Start>
                  <Duration>PT1H</Duration>
                  <Series>Swallow</Series>
                  <Title/>
            </Program>
            <Program flag="favorite">
                  <Start>2001-07-05T22:00:00</Start>
                  <Duration>PT1H</Duration>
                  <Series>AllyMcBeal</Series>
                  <Title/>
            </Program>
      </Channel>
      <Channel>
            <Name>Channel 5</Name>
            <Program>
                  <Start>2001-07-05T19:00:00</Start>
                  <Duration>PT30M</Duration>
                  <Series>MovieChartShow</Series>
                  <Title/>
            </Program>
            <Program>
                  <Start>2001-07-05T19:30:00</Start>
                  <Duration>PT30M</Duration>
                  <Series>5News</Series>
                  <Title/>
            </Program>
            <Program type="entertainment">
                  <Start>2001-07-05T20:00:00</Start>
                  <Duration>PT1H</Duration>
                  <Series/>
                  <Title>The World's Worst Drivers Caught On Tape</Title>
            </Program>
            <Program type="film">
                  <Start>2001-07-05T21:00:00</Start>
                  <Duration>PT1H55M</Duration>
                  <Series/>
                  <Title>Black and White</Title>
            </Program>
            <Program>
                  <Start>2001-07-05T21:55:00</Start>
                  <Duration>PT30M</Duration>
                  <Series>Brookside</Series>
                  <Title/>
            </Program>
      </Channel>
</TVGuide>
=============================================================================
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;

public class DisplayElementNodes {
  static public void main(String[] arg){
    try{
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter a XML file name: ");
      String xmlFile = bf.readLine();
      File file = new File(xmlFile);
      if (file.exists()){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        TransformerFactory tranFactory = TransformerFactory.newInstance(); 
        Transformer aTransformer = tranFactory.newTransformer(); 
        
//=============================================================================================================
        
        // Get nodes list of all elements
        NodeList list = doc.getElementsByTagName("Series");
       
        
        for (int i=0; i<list.getLength(); i++){
          // Get element
          Element element = (Element)list.item(i);
          Source src = new DOMSource(element); 
          System.out.println("Node no: " + i + " is " + element.getNodeName());
          
       
          System.out.println( "Its corresponding xml representation:");
          Result dest = new StreamResult(System.out);
          aTransformer.transform(src, dest);
          System.out.println("\n");
        }
      }
      else{
        System.out.println(xmlFile + " (file name) doesn't found!");
      }
    }
    catch (Exception e){
      e.getMessage();
    }
  }
}

Answer : Find siblings and parent nodes in DOM

actually it wont return you the sibling directly... since there are text nodes in between,
for eg,

to find Start Node's value you need to have somethin like this,

String findStart (Node series){

    Node startNode = series.getPreviousSibling() ;
   if ( startNode.getNodeName.eqauls("Start") ){
     return startNode.getChildNodes().items(0).getNodeValue();
   }else{
     return  findStartNode (statrNode);
   }
}

i have shown only for previous siblings.. probably you will need to go thro NextSiblings also,
Random Solutions  
 
programming4us programming4us