Java Read XML file

Source: Internet
Author: User

    1. The XML file is designed to transmit and store data with the focus on the data content.
    2. HTML is designed to display data with focus on the appearance of the data.
    3. XML is just a text file that can be edited by any text editor in general.
    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.