How to Process XML & JDOM in Java

The JDOM Java SDK lets you parse and read XML from the Internet or a file stored on a user's hard drive. You must open the file and create a "builder" variable that parses the XML from the loaded data. You use this process when you have a file on a Web server that you want to use to import data to display to your readers.

Instructions

    • 1

      Open the Eclipse Java programming platform. Open your Java JDOM project you want to use to parse the XML.

    • 2

      Open the XML file and create a file handler that contains the contents of the file. The following code creates the handler:

      File filehandler = new File("c:\\thefile.xml");

      Replace "thefile.xml" with the location and name of your XML file.

    • 3

      Load the XML data into the parser. The following code loads the data into a "builder" that parses the XML tags so you can extract the data from the tags:

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document xmldoc = builder.parse(file);
      xmldoc.getDocumentElement().normalize();

    • 4

      Display the data from the XML nodes. The following code writes the first node data to the user's window:

      NodeList list = xmldoc.getElementsByTagName("employees");
      Node internalNodes= list.item(0);
      Element e = (Element) internalNodes;
      System.out.println("Employee Name : " + getTagValue("empname", e));

Related Searches:

References

Comments

Related Ads

Featured