Java XML Parse String parse(String xml)

Here you can find the source of parse(String xml)

Description

parse

License

Open Source License

Declaration

protected static Document parse(String xml) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
     /*from   w  w  w .ja v  a2s.co m*/
 *******************************************************************************/

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    protected static Document parse(String xml) {
        if ((xml == null) || (xml.length() == 0))
            return null;

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            StringReader reader = new StringReader(xml);
            InputSource source = new InputSource(reader);
            source.setEncoding("UTF-8"); //$NON-NLS-1$
            return builder.parse(source);
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. parse(String line)
  2. parse(String s)
  3. parse(String text)
  4. parse(String xml)
  5. parse(String xml)
  6. parse(String xml)
  7. parse(String xml)
  8. parseAccessNumber(String line)
  9. parseAddressLines(String[] addressLines)