Example of using Dom to read XML files (Java)

Source: Internet
Author: User
Document document = NULL;
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Try {

Documentbuilder builder = factory. newdocumentbuilder ();

Document = builder. parse (configpath );

Node rootnode = Document. getfirstchild ();

System. Out. println (rootnode. tostring ());

Nodelist logfilelist = rootnode. getchildnodes ();

For (INT I = 1; I <logfilelist. getlength (); I ++ = 2 ){

Node curnode = logfilelist. item (I );

System. Out. println ("Log File" + I + ":" + curnode. tostring ());

Namednodemap map = curnode. getattributes ();

For (Int J = 0; j <map. getlength (); j ++ ){

System. Out. println ("Log File" + I + ": Key:" + map. Item (j). getnodename ());

System. Out. println ("Log File" + I + ": Value:" + map. Item (j). getnodevalue ());

}

Nodelist columns = curnode. getchildnodes ();

For (Int J = 1; j <columns. getlength (); j + = 2 ){

System. Out. println ("column" + J + ":" + columns. Item (j). tostring ());

}

}

} Catch (parserconfigurationexception e ){

E. printstacktrace ();

} Catch (saxexception e ){

E. printstacktrace ();

} Catch (ioexception e ){

E. printstacktrace ();

}

XML file content:

<? XML version = "1.0" encoding = "UTF-8"?>

<Log_mapping>

<LOG_FILE filenamepattern = "ad. + \. log" class = "com.nec.jp. bigm. log. adlogcounter">

<Column name = "date"/>

<Column name = "carrier"/>

<Column name = ""/>

</LOG_FILE>

<LOG_FILE filenamepattern = "access. + \. log" class = "com.nec.jp. bigm. log. adlogcounter">

<Column name = ""/>

<Column name = ""/>

<Column name = ""/>

</LOG_FILE>

</Log_mapping>

Related Article

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.