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

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

Description

Read XML File

License

Open Source License

Parameter

Parameter Description
in_FileName a parameter
in_TagName a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static String ReadXMLFile1(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.NodeList;

public class Main {
    /**/* ww w. jav  a 2  s. c  o  m*/
     * 
     * @param in_FileName
     * @param in_TagName
     * @return
     * @throws Exception
     */
    public static String ReadXMLFile1(String in_FileName, String in_TagName)
            throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(in_FileName);
        Element root = doc.getDocumentElement();

        return GetParStrValue(root, in_TagName);

    }

    public static String GetParStrValue(Element in_el, String in_ParName) {
        NodeList fields = in_el.getElementsByTagName(in_ParName);
        String m_value = "";
        if (fields.getLength() == 1) {
            if (fields.item(0).getFirstChild() != null) {
                m_value = fields.item(0).getFirstChild().getNodeValue();
            }
        }
        if (m_value == null) {
            m_value = "";
        }
        return m_value;
    }
}

Related

  1. readXMLFile(File file)
  2. readXMLFile(String filename)
  3. readXmlFile(String fileName)
  4. readXmlFile(String fileName)
  5. readXMLFile(String filename)
  6. ReadXMLFile3(String in_FileName, String in_TagName)