Thanks Binu.

The code is not even compiling

xml is 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE WEATHER SYSTEM "Weather.dtd">
<WEATHER>
  <CITY NAME="New York">
    <TEMP>64
  </CITY>
</WEATHER>

dtd is 
<!ELEMENT WEATHER (CITY)+>
<!ELEMENT CITY (TEMP)>
<!ATTLIST CITY NAME CDATA #REQUIRED>
<!ELEMENT TEMP (#PCDATA)>  

code is 

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.IOException;

//  A Simple DOM Application
public class BasicDOM {
   
   // Constructor
   public BasicDOM (String xmlFile) {
      
      //  Create a Xerces DOM Parser
      DOMParser parser = new DOMParser();

      //  Parse the Document     
      //  and traverse the DOM
      try {
         parser.parse(xmlFile);
         Document document = parser.getDocument();
         traverse (document);
      } catch (SAXException e) {
         System.err.println (e);
      } catch (IOException e) {
         System.err.println (e);
      }
   }
   
   //  Traverse DOM Tree.  Print out Element Names
   private void traverse (Node node) {
      int type = node.getNodeType();
      if (type == Node.ELEMENT_NODE)
                System.out.println(getElementTextValue(node));
        
      NodeList children = node.getChildNodes();
      if (children != null) {
         for (int i=0; i< children.getLength(); i++) 
            traverse (children.item(i));  
      }
   }
   
   
   private String getElementTextValue(Node e) {
        Node child = e.getFirstChild();
        if (child.getNodeType() == Node.TEXT_NODE){
                ((Text)child).getData();
        } 
        return null;
   }

   
   
   
   // Main Method
   public static void main (String[] args) {
      BasicDOM basicDOM = new BasicDOM (args[0]);
   }
}













---- Original message ----
>Date: Thu, 26 Jul 2001 17:11:22 -0700
>From: Bin Xu <[EMAIL PROTECTED]>  
>Subject: RE: node value  
>To: "'[EMAIL PROTECTED]'" <xerces-j-
[EMAIL PROTECTED]>
>
>Try this:
>
>   private String getElementTextValue(Node e) {
>       Node child = e.getFirstChild();
>       if (childe.getNodeType() == Node.TEXT_NODE)
>               return ((Text)child).getData();
>       } 
>       return null;
>   }
>
>>   private void traverse (Node node) {
>>      int type = node.getNodeType();
>>      if (type == Node.ELEMENT_NODE)
>>         System.out.println (node.getNodeName());
>>        System.out.println(getElementTextValue(node));
>>      NodeList children = node.getChildNodes();
>>      if (children != null) {
>>         for (int i=0; i< children.getLength(); i++)
>>            traverse (children.item(i));
>>      }        
>>   }
>
>
>-----Original Message-----
>From: [EMAIL PROTECTED] 
[mailto:[EMAIL PROTECTED]]
>Sent: Thursday, July 26, 2001 5:04 PM
>To: [EMAIL PROTECTED]
>Subject: RE: node value
>
>
>Thanks
>I have xml tags of element type only. how can we fetch value 
>in Node.ELEMENT_NODE.
>
>I have dtd of type
>
><!ELEMENT no (code, reason)>
><!ELEMENT code (#PCDATA)>
><!ELEMENT reason (#PCDATA)>
>
>Thanks
>vijay 
>
>---- Original message ----
>>Date: Thu, 26 Jul 2001 18:28:14 -0400
>>From: "Swarajit Ray" <[EMAIL PROTECTED]>  
>>Subject: RE: node value  
>>To: <[EMAIL PROTECTED]>
>>
>>Instead of element node , look for text node as the node 
>values are kept as
>>text node in the DOM. Otherwords once you get the 
>Node.ELEMENT_NODE, look
>>for Node.TEXT_NODE and use getNodeValue for the text node.
>>
>>
>>-----Original Message-----
>>From: [EMAIL PROTECTED] 
>[mailto:[EMAIL PROTECTED]]
>>Sent: Thursday, July 26, 2001 5:40 PM
>>To: [EMAIL PROTECTED]
>>Subject: node value
>>
>>
>>Hi
>>This is a simple programwhich takes file from command line.
>>it parses the xml file and should give node and their value.
>>Node are printed and not the values. can anybody help me in
>>getting the values.
>>
>>Thanks
>>vijay
>>
>>
>>
>>
>>
>>import org.apache.xerces.parsers.DOMParser;
>>import org.w3c.dom.Document;
>>import org.w3c.dom.Node;
>>import org.w3c.dom.Element;
>>import org.w3c.dom.NodeList;
>>import org.xml.sax.SAXException;
>>import java.io.IOException;
>>
>>//  A Simple DOM Application
>>public class BasicDOM {
>>
>>   // Constructor
>>   public BasicDOM (String xmlFile) {
>>
>>      //  Create a Xerces DOM Parser
>>      DOMParser parser = new DOMParser();
>>
>>      //  Parse the Document
>>      //  and traverse the DOM
>>      try {
>>         parser.parse(xmlFile);
>>         Document document = parser.getDocument();
>>         traverse (document);
>>      } catch (SAXException e) {
>>         System.err.println (e);
>>      } catch (IOException e) {
>>         System.err.println (e);
>>      }
>>   }
>>
>>   //  Traverse DOM Tree.  Print out Element Names
>>   private void traverse (Node node) {
>>      int type = node.getNodeType();
>>      if (type == Node.ELEMENT_NODE)
>>         System.out.println (node.getNodeName());
>>        System.out.println(node.getNodeValue());
>>      NodeList children = node.getChildNodes();
>>      if (children != null) {
>>         for (int i=0; i< children.getLength(); i++)
>>            traverse (children.item(i));
>>      }
>>   }
>>
>>   // Main Method
>>   public static void main (String[] args) {
>>      BasicDOM basicDOM = new BasicDOM (args[0]);
>>   }
>>}
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to