Java XML Document from File ReadXMLFile3(String in_FileName, String in_TagName)

Here you can find the source of ReadXMLFile3(String in_FileName, String in_TagName)

Description

Read XML File

License

Open Source License

Declaration

public static String ReadXMLFile3(String in_FileName, String in_TagName)
            throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String ReadXMLFile3(String in_FileName, String in_TagName)
            throws Exception {
        String strOut = "";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(in_FileName);
        Element root = doc.getDocumentElement();
        NodeList books = root.getChildNodes();
        if (books != null) {
            for (int i = 0; i < books.getLength(); i++) {
                Node book = books.item(i);
                if (book.getNodeType() == Node.ELEMENT_NODE) {
                    if (book.getNodeName().equals(in_TagName)) {
                        strOut = book.getFirstChild().getNodeValue();
                    }//from   w ww.j a v  a  2s . c  o  m
                }
            }
        }
        return strOut;

    }
}

Related

  1. readXMLFile(String filename)
  2. readXmlFile(String fileName)
  3. readXmlFile(String fileName)
  4. readXMLFile(String filename)
  5. ReadXMLFile1(String in_FileName, String in_TagName)