Appending new tag to the existing xml using java -
my source code needs append/add new tags between existing tags xml document have on hard-disk. confused kind of parser need use complete task.
xml document have looks similar to:
<school> <teacher> <name>xxxxx</name> <gender>xxxx</gender> </teacher> </school>
need xml document be:
<school> <teacher> <name>xxxxx</name> <gender>xxxx</gender> </teacher> <!--need append student tag--> <student> <name>xxxxxx</name> <gender>xxxxx</name> </student> </school>
so, please me in choosing efficient xmlparser in achieving job.also, appreciate if can show me sample source code achieve task.
thanks in advance..
if i'm understanding question correctly, i'm assuming you're trying take xml document contains teachers, , teachers, you'd add corresponding students.
i'd suggest using dom parser (link @ bottom reference).
i've created code allow take xml file teacher information, , generate new 1 desired result.
i used excessive amount of comments convenience!
package com.yourpackage.model; import java.io.file; import java.util.arraylist; import java.util.list; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.transform.transformer; 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; public class addstudentsxml { public static void main(string[] args) throws exception { //create documentbuilderfactory documentbuilderfactory documentbuilderfactory = documentbuilderfactory.newinstance(); documentbuilder documentbuilder = documentbuilderfactory.newdocumentbuilder(); //parse xml file document document = documentbuilder.parse("yourfilename.xml"); //insert file name here //declare school wish populate school school = new school(); //create list of teachers list<teacher> teachers = new arraylist<teacher>(); //create list of nodes document nodelist nodelist = document.getdocumentelement().getchildnodes(); //populate teacher list of teachers in xml document (int = 0; < nodelist.getlength(); i++) { node node = nodelist.item(i); if (node instanceof element) { teacher newteacher = new teacher(); newteacher.name = node.getattributes().getnameditem("name").getnodevalue(); nodelist childnodes = node.getchildnodes(); (int j = 0; j < childnodes.getlength(); j++) { node cnode = childnodes.item(j); if (cnode instanceof element) { string content = cnode.getlastchild().gettextcontent().trim(); if(cnode.getnodename().equals("name")) { newteacher.name = content; } else if(cnode.getnodename().equals("gender")) { newteacher.gender = content; } } } //add new teachers teacher list teachers.add(newteacher); } } //add teachers school school.setteachers(teachers); //create list of students list<student> students = new arraylist<student>(); //populate students here depending upon how data feeding them in //add students school school.setstudents(students); //now start create new xml document exported document newdoc = documentbuilder.newdocument(); //create root of doucment element root = newdoc.getdocumentelement(); //create school element element newschool = newdoc.createelement("school"); //add school root of document root.appendchild(newschool); //for of teachers in teacher list, add corresponding students (teacher teacher : teachers) { //create teacher element element newteacher = newdoc.createelement("teacher"); //add teacher's name element teachername = newdoc.createelement("name"); teachername.appendchild(newdoc.createtextnode(teacher.getname())); newteacher.appendchild(teachername); //add teacher's gender element teachergender = newdoc.createelement("gender"); teachergender.appendchild(newdoc.createtextnode(teacher.getgender())); newteacher.appendchild(teachergender); //add teacher school newschool.appendchild(newteacher); (student student : students) { if (student.getteachername().trim().equals(teacher.getname().trim())) { //create student element element newstudent = newdoc.createelement("student"); //add student's name element studentname = newdoc.createelement("name"); studentname.appendchild(newdoc.createtextnode(student.getname())); newstudent.appendchild(studentname); //add student's gender element studentgender = newdoc.createelement("gender"); studentgender.appendchild(newdoc.createtextnode(student.getgender())); newstudent.appendchild(studentgender); //add student school newschool.appendchild(newstudent); } } } //create transformerfactory, transformer, , result saved new file "schoolinfo.xml" transformerfactory transformerfactory = transformerfactory.newinstance(); transformer transformer = transformerfactory.newtransformer(); domsource source = new domsource(newdoc); streamresult result = new streamresult(new file("c://schoolinfo.xml")); transformer.transform(source, result); system.out.println("file saved!"); } public static class school { private list<teacher> teachers; private list<student> students; public void setteachers(list<teacher> teachers) { this.teachers = teachers; } public void setstudents(list<student> students) { this.students = students; } public list<teacher> getteachers() { return teachers; } public list<student> getstudents() { return students; } } public static class teacher { private string name = ""; private string gender = ""; public string getname() { return name; } public string getgender() { return gender; } } public static class student { private string studentname = ""; private string studentgender = ""; private string teachername = ""; public string getname() { return studentname; } public string getgender() { return studentgender; } public string getteachername() { return teachername; } } }
this should read in xml file (you'll have replace file name actual file name) , populate list of teachers. far student information goes, you'll have populate list wherever retrieving data from.
once data available , stored properly, new document created , xml should appended document in form desired. document stored c: drive, can change path store wherever you'd like.
i used these links references:
mkyong (exporting xml file): here
stackoverflow (appending nodes): here
java code geeks (dom parsers): here
let me know if there in solution needs clarification, i'm new stackoverflow!
Comments
Post a Comment