Package javax.xml.parsers

Examples of javax.xml.parsers.DocumentBuilder


    }
    if (docBuilderFactory == null) {
      docBuilderFactory = DocumentBuilderFactory.newInstance();
      docBuilderFactory.setValidating(false);
    }
    DocumentBuilder constructor = docBuilderFactory.newDocumentBuilder();
    Document doc = constructor.parse(f);
    String loggerName;
    String message;
    String sourceClassName;
    String sourceMethodName;
    int level;


   * @param xmlFile The XML file to load.
   * @return The XML document of the file.
   * @throws RegainException If loading the XML file failed.
   */
  public static Document loadXmlDocument(File xmlFile) throws RegainException {
    DocumentBuilder builder;
    try {
      builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    }
    catch (Exception exc) {
      throw new RegainException("Creating XML document builder failed!", exc);
    }

    Document doc;
    FileInputStream stream = null;
    try {
      stream = new FileInputStream(xmlFile);
      doc = builder.parse(stream);
    }
    catch (Exception exc) {
      throw new RegainException("Parsing XML failed: "
                                + xmlFile.getAbsolutePath(), exc);
    }

  {

    /*
     * 1. Creating the root element
     */
    DocumentBuilder builder = getDocumentBuilder(system);
    Document doc = builder.newDocument();
    Element root = doc.createElement(mEncodingSystem);
    doc.appendChild(root);

    /*
     * 2. Getting the interface description
     */
    String interfaceDescriptionFilename = getInterfaceDescriptionFilename(mEncodingSystem);
    Document interfaceDescription = null;
    try
    {
      interfaceDescription = builder.parse(interfaceDescriptionFilename);
    }
    catch (IOException e)
    {
      throw new XException(Constants.LOCATION_EXTERN,
          Constants.LAYER_PROTOCOL,

    private Element parse(InputSource source) {
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            builderFactory.setNamespaceAware(true);
            builderFactory.setValidating(false);
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            builder.setErrorHandler(new ErrorHandler() {
                    public void error(SAXParseException e)
                        throws SAXParseException {
                        throw e;
                    }

                    public void fatalError(SAXParseException e)
                        throws SAXParseException {
                        throw e;
                    }

                    public void warning(SAXParseException err)
                        throws SAXParseException {
                        // do nothing
                    }
                });

            // builder.setEntityResolver(new NullEntityResolver());
            return builder.parse(source).getDocumentElement();
        } catch (ParserConfigurationException e) {
            throw new ToolException("parsing.parserConfigException", e);
        } catch (FactoryConfigurationError e) {
            throw new ToolException("parsing.factoryConfigException", e);
        } catch (SAXException e) {

    HandlerChainDocument(InputStream is, boolean doValidate) {
        chains = new ArrayList<HandlerChainType>();
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(false);
            DocumentBuilder builder = dbf.newDocumentBuilder();
            Document srcDoc = builder.parse(is);
            dbf.setNamespaceAware(true);
            Document destDoc = builder.newDocument();
            transform(srcDoc, destDoc);

            NodeList chainNodes = destDoc.getFirstChild().getChildNodes();
            for (int i = 0; i < chainNodes.getLength(); i++) {
                Node node = chainNodes.item(i);

   private Document getDocument(String xml) throws XmlBlasterException {
      try {  
         java.io.StringReader reader = new java.io.StringReader(xml);
         InputSource input = new InputSource(reader);
         DocumentBuilderFactory factory = glob.getDocumentBuilderFactory();
         DocumentBuilder builder = factory.newDocumentBuilder ();
         return builder.parse(input)
      } catch (org.xml.sax.SAXException ex) {
         String reason = ex.getMessage();
         if(ex instanceof org.xml.sax.SAXParseException) {
            org.xml.sax.SAXParseException s = (org.xml.sax.SAXParseException)ex;
            reason = reason + " at line="+s.getLineNumber() + " column=" +

         DocumentBuilderFactory dbf = requestBroker.getServerScope().getDocumentBuilderFactory();
         //dbf.setNamespaceAware(true);
         //dbf.setCoalescing(true);
         //dbf.setValidating(false);
         //dbf.setIgnoringComments(true);
         DocumentBuilder db = dbf.newDocumentBuilder ();
         xmlKeyDoc = db.parse(input);
      } catch (Exception e) {
         log.severe("Problems when building DOM tree from your XmlKey: " + e.toString());
         throw new XmlBlasterException(serverScope, ErrorCode.INTERNAL_ILLEGALSTATE, ME, "Problems when building DOM tree from your XmlKey: " + e.toString());
      }
   }

      //input.setEncoding("ISO-8859-2");
      //input.setSystemId("9999999999");

      try {
         DocumentBuilderFactory dbf = glob.getDocumentBuilderFactory();
         DocumentBuilder db = dbf.newDocumentBuilder();
         Document xmlDoc = db.parse(input);
        
         ByteArrayOutputStream out = XmlNotPortable.writeNode(xmlDoc.getDocumentElement());
         String response = new String(out.toByteArray());
         log.info(response);
         reader = new StringReader(response);
         input = new InputSource(reader);
         Document xmlDoc1 = db.parse(input);
         this.assertXMLEqual("", xmlDoc, xmlDoc1);
      }
      catch (ParserConfigurationException e) {
         log.severe("Problems when building DOM parser: " + e.toString() + "\n" + txt);
         throw new XmlBlasterException(glob, ErrorCode.RESOURCE_CONFIGURATION, ME, "Problems when building DOM tree from your XML-ASCII string\n" + txt, e);

   public static void main(String[] args)
   {
      SvgIdMapper mapper = new SvgIdMapper();
      try {
         File file = new File("simple.svg");
         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
         Document doc = builder.parse(file);
         Hashtable idTable = mapper.createIdTable(doc);
         Enumeration keys = idTable.keys();
         while (keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            System.out.println(key);

     */
    private Document urlToDocument(String url)
        throws Exception
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new XslTransformerEntityResolver(url));
        return builder.parse(new InputSource(url));
    }

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.