Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
swdev:java:files:xml_file_routines [2017/10/03 08:12] smayr created |
swdev:java:files:xml_file_routines [2017/10/03 08:22] (current) smayr |
||
---|---|---|---|
Line 21: | Line 21: | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | Read the list of items found in XML file: | ||
+ | <code java> | ||
+ | package com.acme.myproj; | ||
+ | |||
+ | import javax.xml.parsers.DocumentBuilderFactory; | ||
+ | import javax.xml.parsers.DocumentBuilder; | ||
+ | import org.w3c.dom.Document; | ||
+ | import org.w3c.dom.NodeList; | ||
+ | import org.w3c.dom.Node; | ||
+ | import org.w3c.dom.Element; | ||
+ | import java.io.File; | ||
+ | |||
+ | public class XmlFileUtils { | ||
+ | |||
+ | public static void ReadXmlFile(String aFileName) { | ||
+ | |||
+ | try { | ||
+ | |||
+ | File fXmlFile = new File(aFileName); | ||
+ | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | ||
+ | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | ||
+ | Document doc = dBuilder.parse(fXmlFile); | ||
+ | |||
+ | // Optional, but recommended | ||
+ | // Read this - http:// | ||
+ | doc.getDocumentElement().normalize(); | ||
+ | |||
+ | System.out.println(" | ||
+ | |||
+ | NodeList nList = doc.getElementsByTagName(" | ||
+ | |||
+ | System.out.println(" | ||
+ | |||
+ | for (int temp = 0; temp < nList.getLength(); | ||
+ | |||
+ | Node nNode = nList.item(temp); | ||
+ | |||
+ | System.out.println(" | ||
+ | |||
+ | if (nNode.getNodeType() == Node.ELEMENT_NODE) { | ||
+ | |||
+ | Element eElement = (Element) nNode; | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | } | ||
+ | } catch (Exception e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | To read the list of items and their children in XML file, loop through the nodes: | ||
+ | <code java> | ||
+ | package com.acme.myproj; | ||
+ | |||
+ | import java.io.File; | ||
+ | import javax.xml.parsers.DocumentBuilder; | ||
+ | import javax.xml.parsers.DocumentBuilderFactory; | ||
+ | import org.w3c.dom.Document; | ||
+ | import org.w3c.dom.NamedNodeMap; | ||
+ | import org.w3c.dom.Node; | ||
+ | import org.w3c.dom.NodeList; | ||
+ | |||
+ | public class XmlFileUtils | ||
+ | { | ||
+ | |||
+ | public static void ReadAllNodes(String aFileName) | ||
+ | { | ||
+ | |||
+ | try { | ||
+ | |||
+ | File file = new File(aFileName); | ||
+ | |||
+ | DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance() | ||
+ | | ||
+ | |||
+ | Document doc = dBuilder.parse(file); | ||
+ | |||
+ | System.out.println(" | ||
+ | |||
+ | if (doc.hasChildNodes()) { | ||
+ | |||
+ | printNote(doc.getChildNodes()); | ||
+ | |||
+ | } | ||
+ | |||
+ | } catch (Exception e) { | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | private static void printNote(NodeList nodeList) | ||
+ | { | ||
+ | |||
+ | for (int count = 0; count < nodeList.getLength(); | ||
+ | |||
+ | Node tempNode = nodeList.item(count); | ||
+ | |||
+ | // make sure it's element node. | ||
+ | if (tempNode.getNodeType() == Node.ELEMENT_NODE) { | ||
+ | |||
+ | // get node name and value | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | if (tempNode.hasAttributes()) { | ||
+ | // get attributes names and values | ||
+ | NamedNodeMap nodeMap = tempNode.getAttributes(); | ||
+ | |||
+ | for (int i = 0; i < nodeMap.getLength(); | ||
+ | |||
+ | Node node = nodeMap.item(i); | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if (tempNode.hasChildNodes()) { | ||
+ | // loop again if has child nodes | ||
+ | printNote(tempNode.getChildNodes()); | ||
+ | |||
+ | } | ||
+ | |||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | From other code, call routines as follows: | ||
+ | <code java> | ||
+ | private void OpenXmlDataFile() | ||
+ | { | ||
+ | Path pth = Paths.get(System.getProperty(" | ||
+ | XmlFileUtils.ReadAllNodes(pth.toString()); | ||
+ | } | ||
+ | </ | ||
References: | References: |