Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2013
    Posts
    63
    Rep Power
    0

    Default How to add new attributes to an XML node ?

    Hi!
    I've the following xml file:
    Java Code:
    <definitions id="HelloWorld" targetNamespace="http://www.omg.org/bpmn20"
                 xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL">
     
        <process id="helloWorld" name="Hello World">
            <startEvent id="start" name="Start">
                <outgoing>start2sayHello</outgoing>
            </startEvent>
            <userTask id="sayHello" name="Say Hello">
                <documentation>Say "Hello, World!"</documentation>
                <incoming>start2sayHello</incoming>
                <outgoing>sayHello2end</outgoing>
            </userTask>
     
            <sequenceFlow id="start2sayHello" sourceRef="start" targetRef="sayHello"/>
     
            <endEvent id="end" name="End">
                <incoming>sayHello2end</incoming>
            </endEvent>
     
            <sequenceFlow id="sayHello2end" sourceRef="sayHello" targetRef="end" />
        </process>
    </definitions>
    I want to add a new attribute to the process node. For example the attribute groupid= "" .
    I've the following code:
    Java Code:
    public static void displayNodes(NodeList nl) throws JSONException {
    		for (int i = 0; i < nl.getLength(); i++) {
    			Node n = nl.item(i);
    			if (n.getNodeName() == "process") {
                    
    			JSONWriter jw = new JSONStringer().object();
    				if (n.hasAttributes()) {
    					NamedNodeMap nnm = n.getAttributes();
    					
    					
    					if (nnm != null) {
    						
    						for (int j = 0; j < nnm.getLength(); j++) {
    							
                               if(nnm.item(j).getNodeName()=="id"){
                            	   nnm.item(j).setNodeValue(new UID().toString());
    							  }
    							jw.key(nnm.item(j).getNodeName().toString()).value(
    									nnm.item(j).getNodeValue());
    							
    								
    						}
    
    					}
    					
    				}
    This helps me to retrieve all the attributes of the process node but I don't know how to add new attributes ...

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: How to add new attributes to an XML node ?

    One coding problem I see is the code uses == to compare Strings. It should use the equals() method to compare Strings.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Jan 2013
    Posts
    63
    Rep Power
    0

    Default Re: How to add new attributes to an XML node ?

    Ok you're right.
    Any idea about my problem?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: How to add new attributes to an XML node ?

    Lack of knowledge is not a problem - you solve it yourself by reading and learning. Have you made an attempt yet to find and read the documentation of whatever API you're using?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Join Date
    Jan 2013
    Posts
    63
    Rep Power
    0

    Default Re: How to add new attributes to an XML node ?

    Yes I tried to use DOM Parser to modify the xml file.

    My code:
    Java Code:
    public static void displayNodes(NodeList nl) throws JSONException, TransformerException {
    		for (int i = 0; i < nl.getLength(); i++) {
    			Node n = nl.item(i);
    			if (n.getNodeName() == "process") {
    				Xml xml = new Xml("SampleExample.xml");
    				Document doc = xml.parseFromXML("SampleExample.xml");
    				
    				
    						
    				JSONWriter jw = new JSONStringer().object();
    				if (n.hasAttributes()) {
    					NamedNodeMap nnm = n.getAttributes();
    					
    
    					Element process = doc.getElementById("id");
    					process.setAttribute("modelgroup", "");
    					n.appendChild(process);
    					
    					TransformerFactory transformerFactory = TransformerFactory.newInstance();
    					Transformer transformer = transformerFactory.newTransformer();
    					DOMSource source = new DOMSource(doc);
    					StreamResult result = new StreamResult(new File("C:\\Users\\Blerta\\workspaceKEPLER\\XML_to_JSON\\SampleExample.xml"));
    					transformer.transform(source, result);
    					if (nnm != null) {
    						
    						for (int j = 0; j < nnm.getLength(); j++) {
    							
                               if(nnm.item(j).getNodeName()=="id"){
                            	   nnm.item(j).setNodeValue(new UID().toString());
    							  }
    							jw.key(nnm.item(j).getNodeName().toString()).value(
    									nnm.item(j).getNodeValue());
    							
    								
    						}
    
    					}
    					
    				}
    But i get the following exception
    Exception in thread "main" java.lang.NullPointerException
    at Main.displayNodes(Main.java:64)
    at Main.displayNodes(Main.java:136)
    at Main.main(Main.java:32)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: How to add new attributes to an XML node ?

    Exception in thread "main" java.lang.NullPointerException
    at Main.displayNodes(Main.java:64)
    There is a variable with a null value on line 64. Look at line 64 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 64 and print out the values of all the variables on that line.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 08-12-2012, 03:15 AM
  2. class attributes vs instance attributes in web apps
    By rdangelo in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-26-2012, 12:39 PM
  3. Replies: 1
    Last Post: 04-16-2012, 02:29 PM
  4. Attributes vs Parameters
    By java4amanda in forum Java Servlet
    Replies: 1
    Last Post: 01-18-2012, 11:46 AM
  5. Parsing XML attributes
    By javanewbie2020 in forum XML
    Replies: 1
    Last Post: 11-02-2010, 12:39 PM

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
  •