Tutorial for SDK Java XML
The Java developer SDK includes libraries for reading and editing XML files. XML lets you import and export data you use on other platforms such as exporting from a Web app to use in a desktop app. XML standards make it possible to transfer data between applications without the need to have each application installed on the computer. The Java SDK reads any standard XML document, so you can import external data.
Instructions
-
-
1
Open your Java Eclipse editor from the Windows program menu. Open the Web or desktop project you want to use to import the XML data.
-
2
Open the XML file. Use the following code to open an XML data file:
File file= new File("c:\\myfile.xml");
Replace "myfile.xml" with the name of the XML file you want to parse.
-
-
3
Create the "builder" class variables that parse the data. The Java SDK automatically parse the XML in the file, so you can extract the information without parsing tags from the actual data. Add the following code to use the Java XML parser:
DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = builder.newDocumentBuilder();
Document xmldoc= parser.parse(file);
xmldoc.getDocumentElement().normalize(); -
4
Import the data. With the XML document parsed, you can import the data. For instance, the following code retrieves the first node and displays the data to the user:
string root = xmldoc.getDocumentElement().getNodeName();
NodeList customers = xmldoc.getElementsByTagName("customer");
Node node1= customers.item(0);
System.out.println("Root Node Name : " + root);
System.out.println("Customer Name : " + getTagValue("name", node1));
-
1