Search in sources :

Example 76 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project tinker by Tencent.

the class JavaXmlUtil method getDocumentBuilder.

/**
     * get document builder
     *
     * @return DocumentBuilder
     */
private static DocumentBuilder getDocumentBuilder() {
    DocumentBuilder documentBuilder = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
    } catch (Exception e) {
        throw new JavaXmlUtilException(e);
    }
    return documentBuilder;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 77 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project malmo by Microsoft.

the class SchemaHelper method getRootNodeName.

/** Retrieve the name of the root node in an XML string.
     * @param xml The XML string to parse.
     * @return The name of the root node, or null if parsing failed.
     */
public static String getRootNodeName(String xml) {
    String rootNodeName = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setExpandEntityReferences(false);
        DocumentBuilder dBuilder = dbf.newDocumentBuilder();
        InputSource inputSource = new InputSource(new StringReader(xml));
        Document doc = dBuilder.parse(inputSource);
        doc.getDocumentElement().normalize();
        rootNodeName = doc.getDocumentElement().getNodeName();
    } catch (SAXException e) {
        System.out.println("SAX exception: " + e);
    } catch (IOException e) {
        System.out.println("IO exception: " + e);
    } catch (ParserConfigurationException e) {
        System.out.println("ParserConfiguration exception: " + e);
    }
    return rootNodeName;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 78 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project Mycat-Server by MyCATApache.

the class FirewallConfig method updateToFile.

public static synchronized void updateToFile(String host, List<UserConfig> userConfigs) throws Exception {
    LOGGER.debug("set white host:" + host + "user:" + userConfigs);
    String filename = SystemConfig.getHomePath() + File.separator + "conf" + File.separator + "server.xml";
    //String filename = "E:\\MyProject\\Mycat-Server\\src\\main\\resources\\server.xml";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new IgnoreDTDEntityResolver());
    Document xmldoc = builder.parse(filename);
    Element whitehost = (Element) xmldoc.getElementsByTagName("whitehost").item(0);
    Element firewall = (Element) xmldoc.getElementsByTagName("firewall").item(0);
    if (firewall == null) {
        firewall = xmldoc.createElement("firewall");
        Element root = xmldoc.getDocumentElement();
        root.appendChild(firewall);
        if (whitehost == null) {
            whitehost = xmldoc.createElement("whitehost");
            firewall.appendChild(whitehost);
        }
    }
    for (UserConfig userConfig : userConfigs) {
        String user = userConfig.getName();
        Element hostEle = xmldoc.createElement("host");
        hostEle.setAttribute("host", host);
        hostEle.setAttribute("user", user);
        whitehost.appendChild(hostEle);
    }
    TransformerFactory factory2 = TransformerFactory.newInstance();
    Transformer former = factory2.newTransformer();
    String systemId = xmldoc.getDoctype().getSystemId();
    if (systemId != null) {
        former.setOutputProperty(javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, systemId);
    }
    former.transform(new DOMSource(xmldoc), new StreamResult(new File(filename)));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) File(java.io.File)

Example 79 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project OpenAttestation by OpenAttestation.

the class ReadXmlTest method getTagSelectionFromXml.

public TagSelection getTagSelectionFromXml(String xml) throws ParserConfigurationException, SAXException, IOException {
    TagSelection ret = new TagSelection();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    Document doc = builder.parse(is);
    ArrayList<tag> tagList = new ArrayList<tag>();
    int cnt = 0;
    NodeList nodeList = doc.getElementsByTagName("attribute");
    for (int s = 0; s < nodeList.getLength(); s++) {
        Node fstNode = nodeList.item(s);
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
            Element fstElmnt = (Element) fstNode;
            String idValue = fstElmnt.getAttribute("oid");
            Element lstNmElmnt = (Element) nodeList.item(cnt++);
            NodeList lstNm = lstNmElmnt.getChildNodes();
            String currentAction = ((Node) lstNm.item(0)).getNodeValue();
            if (currentAction != null) {
                tagList.add(new tag("", idValue, currentAction));
            }
        }
    }
    nodeList = doc.getElementsByTagName("selection");
    Node fstNode = nodeList.item(0);
    Element e = (Element) fstNode;
    ret.id = e.getAttribute("id");
    ret.name = e.getAttribute("name");
    ret.tagList = tagList;
    return ret;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader)

Example 80 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project OpenGrok by OpenGrok.

the class SubversionRepository method setDirectoryName.

@Override
public void setDirectoryName(String directoryName) {
    super.setDirectoryName(directoryName);
    if (isWorking()) {
        // set to true if we manage to find the root directory
        Boolean rootFound = Boolean.FALSE;
        List<String> cmd = new ArrayList<>();
        cmd.add(RepoCommand);
        cmd.add("info");
        cmd.add("--xml");
        File directory = new File(getDirectoryName());
        Executor executor = new Executor(cmd, directory);
        if (executor.exec() == 0) {
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document document = builder.parse(executor.getOutputStream());
                String url = getValue(document.getElementsByTagName("url").item(0));
                if (url == null) {
                    LOGGER.log(Level.WARNING, "svn info did not contain an URL for [{0}]. Assuming remote repository.", directoryName);
                    setRemote(true);
                } else {
                    if (!url.startsWith("file")) {
                        setRemote(true);
                    }
                }
                String root = getValue(document.getElementsByTagName("root").item(0));
                if (url != null && root != null) {
                    reposPath = url.substring(root.length());
                    rootFound = Boolean.TRUE;
                }
            } catch (SAXException saxe) {
                LOGGER.log(Level.WARNING, "Parser error parsing svn output", saxe);
            } catch (ParserConfigurationException pce) {
                LOGGER.log(Level.WARNING, "Parser configuration error parsing svn output", pce);
            } catch (IOException ioe) {
                LOGGER.log(Level.WARNING, "IOException reading from svn process", ioe);
            }
        } else {
            LOGGER.log(Level.WARNING, "Failed to execute svn info for [{0}]. Repository disabled.", directoryName);
        }
        setWorking(rootFound);
    }
}
Also used : Executor(org.opensolaris.opengrok.util.Executor) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) File(java.io.File) SAXException(org.xml.sax.SAXException)

Aggregations

DocumentBuilder (javax.xml.parsers.DocumentBuilder)883 Document (org.w3c.dom.Document)694 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)622 Element (org.w3c.dom.Element)339 IOException (java.io.IOException)276 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)276 NodeList (org.w3c.dom.NodeList)267 InputSource (org.xml.sax.InputSource)238 SAXException (org.xml.sax.SAXException)235 Node (org.w3c.dom.Node)199 StringReader (java.io.StringReader)167 Test (org.junit.Test)127 DOMSource (javax.xml.transform.dom.DOMSource)102 File (java.io.File)99 ByteArrayInputStream (java.io.ByteArrayInputStream)86 InputStream (java.io.InputStream)73 ArrayList (java.util.ArrayList)72 StreamResult (javax.xml.transform.stream.StreamResult)65 Transformer (javax.xml.transform.Transformer)59 SAXParseException (org.xml.sax.SAXParseException)56