Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2013
    Posts
    64
    Rep Power
    0

    Default How to create new XML DOC with properties from an existing one?

    I have an XML doc that looks like this:
    Java Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <model>
            <id>_1</id>
        	<nodes>
        	<id>_2</id>
        	<stencil>TASK</stencil>
            </nodes>
        	<nodes>
        	<id>_3</id>
        	<stencil>TASK</stencil>
            </nodes>
        </model>
    I have to create another xml doc with the properties of nodes from the first doc. For the new doc I have to create a parent node
    called "definitions". Instead of the "model" node in the first doc I have to create a "process" node in the new doc that has an
    attribute "id" which value is the same as the content of the "id" child node of model. For each "nodes" node in the first doc
    if their "stencil" child node content equals "TASK" I create a "task" node in the new xml doc.
    Java Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <definitions>
          <process id="_1">
            <task id="_2">
            </task>
            <task id="_3">
            </task>
          </process>
        </definitions>
    To achieve this I have created three classes Definitions, Process and Task :
    Java Code:
        public class Definitions {
        	     public Process extractProcess(Document simpleXml_doc) throws XPathExpressionException{
        			Process p = new Process();
        			p.setProcess("process");
        			XPath xPath = XPathFactory.newInstance().newXPath();
        			XPathExpression xPathEx1 = xPath.compile("/model/id");
        			Node n1 = (Node) xPathEx1.evaluate(simpleXml_doc, XPathConstants.NODE);
        			p.setIdProcess(n1.getTextContent());
        			return p;
        		}
        }
    Java Code:
        public class Process {
        	
        	 private String process;
        	 public String getProcess(){
        		 return process;
        	 }
        	 public void setProcess(String process){
        		 this.process = process;
        	 }
        		 private String idProcess;
        			public String getIdProcess(){
        				return idProcess;
        			}
        			public void setIdProcess(String idProcess){
        				this.idProcess = idProcess;
        			}
        			
        			
        	public ArrayList<Task> extractTasks(Document firstXml_doc) throws XPathExpressionException{
        		ArrayList<Task> taskList = new ArrayList<>();
        		XPath xPath = XPathFactory.newInstance().newXPath();
        		XPathExpression xPathEx1 = xPath.compile("/model/nodes/stencil");
        		NodeList nl1 = (NodeList) xPathEx1.evaluate(simpleXml_doc, XPathConstants.NODESET);
        		for(int index=0; index<nl1.getLength(); index++){
        			if(nl1.item(index).getTextContent().equals("TASK")){
        				Task t = new Task();
        				t.setTask("task");
        				XPathExpression xPathEx2 = xPath.compile("/model/nodes/id");
        				NodeList nl2 = (NodeList) xPathEx2.evaluate(simpleXml_doc, XPathConstants.NODESET);
        				t.setIdTask("_" + nl2.item(index).getTextContent());
        				taskList.add(t);
        			}
        		}
        		return taskList;
        	}
        }
    Java Code:
        public class Task {
        	private String task; 
        	public String getTask(){
        		return task;
        	}
        	public void setTask(String task){
        		this.task = task;
        	}
        	//do krijoj properties per atributet e elementit task
            private String idTask;
            private String nameTask;
        	public String getIdTask(){
        		return idTask;
        	}
        	public void setIdTask(String idTask){
        		this.idTask = idTask;
        	}
        }
    I just wanted to know if this is the correct way to define the respective classes.
    Can anyone tell me any way to create and fill the nodes for the new doc using this
    classes?
    I am used with DOM parser and I know how to create nodes and fill attribute values,
    but I have always done this job in a single class, not using different classes for the
    elements.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: How to create new XML DOC with properties from an existing one?

    I must be going crazy. Are you asking here how to use the code you wrote yourself?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: How to create new XML DOC with properties from an existing one?

    Note: Cross-posted at java - How to create new XML DOC with properties from an existing one? - Stack Overflow

    (Next time it'll be helpful to state here other forums where you have cross-posted your question so that if it has already been answered elsewhere, there's no need for anyone to spend time here. Ditto vice versa.)

    I agree with gimbal2... it is quite strange that if you have written the above code, you surely must know how you would be use it. Therefore please provide further context behind your question - what are you working on, and how did the above code come about.

    To help you get started with this, the following is an outline of what you'll need to do.
    1. Read the source XML file, and parse it into a Document object. (See Reading XML Data into a DOM (The Java™ Tutorials > Java API for XML Processing (JAXP) > Document Object Model) if you're unsure how to do this.)
    2. Instantiate a Definitions object.
    3. Call the extractProcess method in the Definitions object to get a Process object. (You can do some println()s against the Process object at this point to check you've done this correctly.)
    4. Call the extractTasks method in the Process object to get an ArrayList of Task objects. (At this point you can iterate through the ArrayList, and println() each Task objet to check you've done this correctly.)

    After the above you'll have everything you need to start creating a new XML document for your target XML file. See How to create XML file in Java – (DOM Parser) if you're unsure how to do this.

    Btw be aware that there are a few bugs in the code you posted...
    Last edited by jashburn; 03-13-2014 at 02:52 PM. Reason: Added note on bugs in posted code.

Similar Threads

  1. how to put existing file in ftp
    By trkece in forum Networking
    Replies: 0
    Last Post: 02-11-2011, 05:42 AM
  2. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  3. Get CSS/ Use existing CSS
    By karthikeyan_raju in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 10-21-2009, 05:24 PM
  4. how to add GUI to existing program?
    By VinTiger in forum New To Java
    Replies: 2
    Last Post: 05-08-2009, 05:05 AM
  5. Create a new directory in existing Zip archive
    By satishbejgum in forum Advanced Java
    Replies: 1
    Last Post: 12-23-2007, 06:05 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •