Package javax.xml.parsers

Examples of javax.xml.parsers.DocumentBuilder


   */
  protected org.w3c.dom.Element createMessage() {
    org.w3c.dom.Element ret=null;
   
    try {
      DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
     
      Document doc=builder.newDocument();
     
      ret = doc.createElement(TOP_LEVEL_ELEMENT_NAME);
     
    } catch(Exception e) {
      logger.error("Failed to create message", e);


    try {
      // Transform the text representation to DOM
      DocumentBuilderFactory fact=DocumentBuilderFactory.newInstance();
      fact.setNamespaceAware(true);
     
      DocumentBuilder builder=fact.newDocumentBuilder();
     
      // Check if XML document, and if not return as text node
      if (text.trim().length() == 0 || text.charAt(0) != '<') {
        org.w3c.dom.Document doc=builder.newDocument();
       
        // Assume is text node
        ret = doc.createTextNode(text);
      } else {
        java.io.InputStream xmlstr=
          new java.io.ByteArrayInputStream(text.getBytes());

        org.w3c.dom.Document doc=builder.parse(xmlstr);
       
        xmlstr.close();
     
        ret = doc.getDocumentElement();
      }

public class XmlUtil {
    private static final DocumentBuilderFactory domBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    private static final WeakHashMap domBuilders = new WeakHashMap();

    private static synchronized DocumentBuilder getDocumentBuilder() {
        DocumentBuilder domBuilder = (DocumentBuilder) domBuilders.get(Thread.currentThread());

        if (domBuilder != null) {
            return domBuilder;
        } else {
            try {

     *
     *
     * @return ...
     */
    public static Document newDocument() {
        DocumentBuilder d = getDocumentBuilder();

        return d.newDocument();
    }

     * @return ...
     *
     * @throws RuntimeException ...
     */
    public static Document parse(InputStream in) throws RuntimeException {
        DocumentBuilder d = getDocumentBuilder();

        try {
            Document doc = d.parse(in);

            doc.normalize();

            return doc;
        } catch (SAXException e) {

     * @return ...
     *
     * @throws RuntimeException ...
     */
    public static Document parse(InputSource in) throws RuntimeException {
        DocumentBuilder d = getDocumentBuilder();

        try {
            Document doc = d.parse(in);

            doc.normalize();

            return doc;
        } catch (SAXException e) {

                                    ParserConfigurationException {
        if (domBuilderFactory == null) {
            domBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
        }

        DocumentBuilder parser = domBuilderFactory.newDocumentBuilder();
        Document doc;

        if (obj instanceof String) {
            try {
                // first try to interpret string as URL
                new URL(obj.toString());

                doc = parser.parse(obj.toString());
            } catch (MalformedURLException nourl) {
                // if not a URL, maybe it is the XML itself
                doc = parser.parse(new InputSource(new StringReader(obj.toString())));
            }
        } else if (obj instanceof InputStream) {
            doc = parser.parse(new InputSource((InputStream) obj));
        } else if (obj instanceof Reader) {
            doc = parser.parse(new InputSource((Reader) obj));
        } else {
            throw new RuntimeException("Unrecognized argument to parseXml: " + obj);
        }

        doc.normalize();

    private static Document buildDocument(InputStream is) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        final Document doc;
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(is);
        } catch (Exception e) {
            throw new IllegalStateException("buildDocument failed!", e);
        }
        return doc;
    }

    {

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(true);
      factory.setValidating(true);
      DocumentBuilder builder = factory.newDocumentBuilder();

      ErrorHandler errorHandler = new ErrorHandler() {

        public void warning(org.xml.sax.SAXParseException e)
        {
        //  System.err.println("Warning : " + e.getMessage());
        }

        public void error(org.xml.sax.SAXParseException e)
        {
        // System.err.println("Error : " + e.getMessage());
        }

        public void fatalError(org.xml.sax.SAXParseException e)
        {
        //  System.err.println("FatalError : " + e.getMessage());
        }
      };
      builder.setErrorHandler(errorHandler);

      if (xml.substring(xml.length() - 3, xml.length()).equals("xml"))
      {
        returnDocument = builder.parse(new File(filePath + xml));
      } else
      {
        InputStream in = new BufferedInputStream(new StringBufferInputStream(xml));
        returnDocument = builder.parse(in);
      }
    } catch (IOException e)
    {
      logger.error("[getDocumentFromString] Exception thrown.", e);
    } catch (ParserConfigurationException e)

        Document doc;
        try {
            // read the schema to DOM representation
            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
            fact.setNamespaceAware(true);
            DocumentBuilder bldr = fact.newDocumentBuilder();
            doc = bldr.parse(m_wsdlProvider.getWSDL(req));
            Element schema = doc.getDocumentElement();
            NodeList services = schema.getElementsByTagNameNS(WSDLSOAP_NAMESPACE, "address");
            for (int i = 0; i < services.getLength(); i++) {
                Node node = services.item(i).getAttributes().getNamedItem("location");
                if (node != null) {

TOP

Related Classes of javax.xml.parsers.DocumentBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.