How to Use Xerces in Eclipse

Xerces is an XML parser for Java, and Eclipse is a programming environment that lets you execute Xerces classes for your XML files. You must instantiate the class in order for Xerces to parse the XML documents. Xerces has several functions that make parsing XML more convenient, and the library works seamlessly with the Java language in Eclipse.

Instructions

    • 1

      Open the Eclipse software and open the Java project you want to use to parse the XML file. Double-click the .java source code file in the left panel to open the code in the editor.

    • 2

      Instantiate the class and open the XML file. The class loads the XML into a handler so you can edit or display the information. The following code loads the file customer.xml into the parser, but replace this file with your own:

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      xml= db.parse("customer.xml");

    • 3

      Display the node list. For instance, the following code displays the employee's first and last name to the user:

      Element elements = xml.getDocumentElement();
      NodeList nodes = elements.getElementsByTagName("customer");
      System.out.println((Element)nodes.item('FirstName') + " " + (Element)nodes.item('LastName') );

Related Searches:

References

Comments

Related Ads

Featured