Top5.xml
java
<?xml version="1.0" encoding="UTF-8"?> <root> <hosts> <host name="api101.beta.lv.com" port="7996" desc="Testing" site="Test_UK"></host> <host name="api102index.beta.lv.com" port="1666" desc="Index server of live site." site="AT"></host> <host name="api103index.beta.lv.com" port="4666" desc="Index server of live site." site="DE"></host> <host name="api104index.beta.lv.com" port="2666" desc="Index server of live site." site="FR"></host> <host name="api105index.beta.lv.com" port="3666" desc="Index server of live site." site="DK"></host> <host name="api106.beta.lv.com" port="6666" desc="Live Web Server 2" site="SE"></host> <host name="api107index.beta.lv.com" port="5666" desc="Index server of live site." site="UK"></host> <host name="api108.beta.lv.com" port="5666" desc="Live Web Server 1" site="UK"></host> <host name="api109.beta.lv.com" port="5666" desc="Live Web Server 2" site="UK"></host> </hosts> </root>
XmlOperation.javaapi
package funcs; import java.io.File; import java.util.LinkedList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class XmlOperation { public static void RetrieveDataFromXml() { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = factory.newDocumentBuilder(); File file = new File("src\\config\\Top5.xml"); Document doc = docBuilder.parse(file); NodeList hostsXml = doc.getElementsByTagName("host"); LinkedList<funcs.Entity.Host> hostsEntity = new LinkedList<funcs.Entity.Host>(); for (int i = 0; i < hostsXml.getLength(); i++) { funcs.Entity.Host hostEntity = new funcs.Entity.Host(); Element hostXml = (Element) hostsXml.item(i); hostEntity.setName(hostXml.getAttribute("name")); hostEntity.setPort(hostXml.getAttribute("port")); hostEntity.setDesc(hostXml.getAttribute("desc")); hostEntity.setSite(hostXml.getAttribute("site")); hostsEntity.add(hostEntity); } if (hostsEntity.size() == 0) { System.out.println("There's no info about any host!"); } else { for (int i = 0; i < hostsEntity.size(); i++) { System.out.println(hostsEntity.get(i).getName() + "\t" + hostsEntity.get(i).getPort() + "\t" + hostsEntity.get(i).getDesc() + "\t" + hostsEntity.get(i).getSite()); } } } catch (Exception ex) { System.out.println(ex.getMessage()); } } }