- The XML file is designed to transmit and store data with the focus on the data content.
- HTML is designed to display data with focus on the appearance of the data.
- XML is just a text file that can be edited by any text editor in general.
- XML does not have a predefined label, and the label is case-sensitive.
The label must also:
-
- Must have root element
- Attribute values need to be quoted
- Spaces, etc. will be preserved
- Label must be closed
Java Read XML file
The contents of the 1,language.xml file are:
<?XML version= "1.0" encoding = "UTF-8"?><languageCat= "It"> <LANID= "1"> <name>Java</name> <IDE>Eclipse</IDE> </LAN> <LANID= "2"> <name>Objective-c</name> <IDE>Xcode</IDE> </LAN> <LANID= "3"> <name>C#</name> <IDE>Visual Studio</IDE> </LAN></language>
2,java Code
Import Org.w3c.dom.document;import org.w3c.dom.element;import Org.w3c.dom.node;import Org.w3c.dom.NodeList;import Org.xml.sax.saxexception;public class Xmlread {public static void main (string[] args) throws Saxexception, IOException {t ry {Documentbuilderfactory factory = documentbuilderfactory.newinstance ();D Ocumentbuilder builder = Factory.newdocumentbuilder ();D ocument Document = builder.parse (New File ("Language.xml")); Element root =document.getdocumentelement (); System.out.println ("<language cat = \" "+root.getattribute (" cat ") +" \ ">"); NodeList list = Root.getelementsbytagname ("LAN"), for (int i=0;i<list.getlength (); i++) {Element lan = (Element) List.item (i); SYSTEM.OUT.PRINTLN ("\t<lan id = \" "+lan.getattribute (" id ") +" \ ">"); NodeList clist = Lan.getchildnodes (); for (int j = 0; J < Clist.getlength (); j + +) {Node c = Clist.item (j); if (c instance of Element) {System.out.println ("\t\t<" +c.getnodename () + ">" +c.gettextcontent () + "</" +c.getnodename () + " > ");}} System.out.println ("\t</lan>");} System.out.println ("</language>");} catch (Parserconfigurationexception e) {}}}
Operation Result:
Java Read XML file