1

I am serach alot of about how I can order my xml file. I create node xml by java, but file xml put my data in same line.

My output java :

<?xml version="1.0" encoding="UTF-8" standalone="no"?><Projects><ProjectDLocation><locationProject>Projects//arfqwre//ProjectDataBase.xml</locationProject><nameProject>arfqwre</nameProject></ProjectDLocation><ProjectDLocation><locationProject>Projects//arfqwre//ProjectDataBase.xml</locationProject><nameProject>arfqwre</nameProject></ProjectDLocation>
</Projects> 

Just I need some way for order my node xml when I insert node: For Example:

       <?xml version="1.0" encoding="UTF-8" standalone="no"?>
        <Projects>
        <ProjectDLocation>
        <locationProject>Projects//arfqwre//ProjectDataBase.xml</locationProject>
    <nameProject>arfqwre</nameProject>
        </ProjectDLocation>
        <ProjectDLocation>
        <locationProject>Projects//arfqwre//ProjectDataBase.xml</locationProject>
<nameProject>arfqwre</nameProject>
        </ProjectDLocation>
        </Projects>

code java:

public void insertNewProjectLocation(Project entity) {

        String filePath = "DataBase//locationProject.xml";
        File xmlFile = new File(filePath);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc;

            doc = dBuilder.parse(xmlFile);

            doc.getDocumentElement().normalize();

            Node projectData = doc.getElementsByTagName("Projects").item(0);

            Element ProjectDLocation = doc.createElement("ProjectDLocation");

            Element locationProject = doc.createElement("locationProject");
            if(entity.getLocation()==null){locationProject.appendChild(doc.createTextNode("Projects" + "//" +entity.getName()+"//"+"ProjectDataBase.xml"));}
            else
            locationProject.appendChild(doc.createTextNode(entity.getLocation()));
            ProjectDLocation.appendChild(locationProject);

            Element nameProject = doc.createElement("nameProject");
            nameProject.appendChild(doc.createTextNode(entity.getName()));
            ProjectDLocation.appendChild(nameProject);
            projectData.insertBefore(ProjectDLocation, projectData.getFirstChild());
            doc.getDocumentElement().normalize();

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource domSource = new DOMSource(doc);
            StreamResult streamResult = new StreamResult(new File("DataBase//locationProject.xml"));
            transformer.transform(domSource, streamResult);

        } catch (ParserConfigurationException pce) {
            return;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException tfe) {
            return;
        }

    }

0 Answers0