2
\$\begingroup\$

I'm trying to make an XML editor. So far, I got the results I wanted, but a lot of code is repeated. How can I reuse more effectively?

import java.io.File;
import java.io.IOException;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlUtil {
    private static File fXmlFile;
    private static DocumentBuilderFactory dbFactory = DocumentBuilderFactory
            .newInstance();
    private static DocumentBuilder dBuilder;
    private static Document doc;

    public static ObservableList<String> getAllTagText(String fileName)
            throws ParserConfigurationException, SAXException, IOException {
        ObservableList<String> tagList = FXCollections.observableArrayList();

        dBuilder = dbFactory.newDocumentBuilder();
        fXmlFile = new File(fileName);
        doc = dBuilder.parse(fXmlFile);

        doc.getDocumentElement().normalize();
        NodeList nodeList = doc.getElementsByTagName("Tag");

        for (int i = 0; i < nodeList.getLength(); i++) {
            String tag = nodeList.item(i).getTextContent();
            tagList.add(tag);
        }

        return tagList;
    }

    public static String getLanguageText(String fileName, String tag,
            String language) throws ParserConfigurationException, SAXException,
            IOException {
        dBuilder = dbFactory.newDocumentBuilder();
        fXmlFile = new File(fileName);
        doc = dBuilder.parse(fXmlFile);

        doc.getDocumentElement().normalize();
        NodeList nodeList = doc.getElementsByTagName("TEXT");

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                if (element.getElementsByTagName("Tag").item(0)
                        .getTextContent().equals(tag)) {
                    return element.getElementsByTagName(language).item(0)
                            .getTextContent();

                }
            }

        }
        return null;
    }

    public static void setText(String fileName, String tag, String textContent)
            throws SAXException, IOException, ParserConfigurationException,
            TransformerException {
        dBuilder = dbFactory.newDocumentBuilder();
        fXmlFile = new File(fileName);
        doc = dBuilder.parse(fXmlFile);

        doc.getDocumentElement().normalize();
        NodeList nodeList = doc.getElementsByTagName("TEXT");

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                if (element.getElementsByTagName("Tag").item(0)
                        .getTextContent().equals(tag)) {
                    element.getElementsByTagName("Hungarian").item(0)
                            .setTextContent(textContent);
                }

            }
            TransformerFactory transformerFactory = TransformerFactory
                    .newInstance();

            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
            DOMSource domSource = new DOMSource(doc);

            StreamResult streamResult = new StreamResult(fXmlFile);
            transformer.transform(domSource, streamResult);
        }

    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

XPath does away with much of the very low-level w3c classes. And is within java SE: javax.xml.xpath.XPath.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.