Results 1 to 20 of 21

Thread: which one more powerful in java ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Nov 2008
    Answers
    2

    Re: which one more powerful in java ?

    can we read a xml file through the core java application?if possible provide one example also?


  2. #2
    Junior Member
    Join Date
    Apr 2009
    Answers
    3

    Re: which one more powerful in java ?

    Quote Originally Posted by hemanthreddy42 View Post
    can we read a xml file through the core java application?if possible provide one example also?
    We can read XML files using DOM Model -- as follows:
    <?xml version="1.0"?>
    <company>
    <employee>
    <firstname>Tom</firstname>
    <lastname>Cruise</lastname>
    </employee>
    <employee>
    <firstname>Paul</firstname>
    <lastname>Enderson</lastname>
    </employee>
    <employee>
    <firstname>George</firstname>
    <lastname>Bush</lastname>
    </employee>
    </company>

    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

    public class XMLReader {

    public static void main(String argv[]) {

    try {
    File file = new File("c:\\MyXMLFile.xml");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);//This can take a string variable also
    doc.getDocumentElement().normalize();
    System.out.println("Root element " + doc.getDocumentElement().getNodeName());
    NodeList nodeLst = doc.getElementsByTagName("employee");
    System.out.println("Information of all employees");

    for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);

    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

    Element fstElmnt = (Element) fstNode;
    NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
    Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    NodeList fstNm = fstNmElmnt.getChildNodes();
    System.out.println("First Name : " + ((Node) fstNm.item(0)).getNodeValue());
    NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");
    Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
    NodeList lstNm = lstNmElmnt.getChildNodes();
    System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
    }

    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    Do try this code just by sending the string [thats the contents of the XML-file].


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact