Results 1 to 6 of 6
  1. #1
    mkj_mca00 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default Generating dynamic XML with arguments.

    Hi,

    Is there any way we can manage the node values with parameters ...
    <details>
    <person>
    <name>{{name_0}}</name>
    <email>{{email_0}}</email>
    </person>
    <address>
    <line1>{{line1_0}}</line1>
    <line2>{{line1_0}}</line2>
    </address>
    <age> {{age_0}}</age>
    </details>

    I have a XML file like above.

    My requirement is like Say If I run execute once then it should create only one <details> with arguments ending with 0 on its child nodes/elements (like shown in xml).
    If i loop twice then it should create 2 nodes one with arguments 0 and the other with arguments 1 as shown above and so on.(like below). It should go on similar fashion with the number of loops.

    <details>
    <person>
    <name>{{name_0}}</name>
    <email>{{email_0}}</email>
    </person>
    <address>
    <line1>{{line1_0}}</line1>
    <line2>{{line1_0}}</line2>
    </address>
    <age> {{age_0}}</age>
    </details>

    <details>
    <person>
    <name>{{name_1}}</name>
    <email>{{email_1}}</email>
    </person>
    <address>
    <line1>{{line1_1}}</line1>
    <line2>{{line1_1}}</line2>
    </address>
    <age> {{age_1}}</age>
    </details>

    Note- This is a huge XML file so I want to avoid the getElementsByTagName as well ..

    Appriciate your help on this and thanks in advance.

    Regards,
    MKJ

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    ?

    Not sure at all what you're getting at here.
    I think you've tried to precis it to the point you've actually lost the purpose.

    What's your source xml look like (and you should be able to give us that) and what do you want your output to look like? And what determines that output?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    How huge your file is not an issue, getElementsByTagName will do the job. The point is that your details node is to be a child of, and may be tag with a node value. If not you've to traverse one by one.

    Anyway as Tolls says, you should provide more details about it.

  4. #4
    mkj_mca00 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you so much for the reply.
    My requirement is to create an xml for automation. It should create the parameter values {{name_0}} ,{{name_1}} ... etc where the parameter values will be taken from Excel.
    Say I want to test one person details then the xml will be created with one details node and the corresponding values for the same will be read out from excel having values with column name name_0, line1_0... If I want to test a scenario with 2 person then the xml file will be created dynamically and it will have two details node and each having different parameter on it ..and the values will be taken from the excel... Below is my code.
    Java Code:
    try { 
    
    DocumentBuilderFactory documentBuilderFactory =
                    DocumentBuilderFactory.newInstance();
                documentBuilderFactory.setValidating(true);
    
      DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();
      Document doc = documentBuilder.parse(new File("D:\\Xml\\book.xml"));
    
           Element root = doc.getDocumentElement( );
    
            NodeList descriptionElements = root.getElementsByTagName("person");
      
    NodeIterator i = ((DocumentTraversal)doc).createNodeIterator(doc.getDocumentElement(), NodeFilter.SHOW_ALL, null, true);
    
     Node n;
     while ((n = i.nextNode( )) != null )
        {
      if ((n.getNodeType( ) == Node.TEXT_NODE ) && (!(n.getNodeValue()==null)) )
                    {
    
                Text discount = (Text)n;
    discount.setNodeValue("{{" + n.getNodeName() + "_01"+ "}}");
                     v.add("Element:- " + discount.getNodeValue());
                              }
    
        }
    
    DOMSource domSource = new DOMSource(doc);
                StreamResult streamResult =new StreamResult(
                    new FileOutputStream( "D:\\Xml\\bookModified.xml"));
                TransformerFactory tFactory = TransformerFactory.newInstance();
    
                Transformer transformer = tFactory.newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT , "yes");
                transformer.transform(domSource,streamResult);
    
    
    }
    The below is my xml
    <?xml version="1.0" encoding="UTF-8"?>
    <Objects>
    <person>
    <first>Kiran</first>
    <last>Pai</last>
    <age>22</age>
    </person>
    <person>
    <first>sdf</first>
    <last>jjj</last>
    <age>34</age>
    </person>
    </Objects>
    Last edited by Eranga; 01-28-2011 at 01:57 AM. Reason: code tags added

  5. #5
    mkj_mca00 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Hi,

    Sorry to mention that this is only changing all the element values of the XML file and the XML file should not have space . But want some good solution that will update the xml in more specific way..

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 8
    Last Post: 09-23-2010, 12:07 AM
  2. generating 26 buttons
    By yasmin k in forum AWT / Swing
    Replies: 4
    Last Post: 11-08-2009, 02:11 PM
  3. Generating a Keystore
    By techbossmb in forum Java Applets
    Replies: 2
    Last Post: 09-30-2009, 07:31 PM
  4. Generating Dynamic Lists
    By jmorris in forum New To Java
    Replies: 3
    Last Post: 12-01-2008, 10:03 PM
  5. Generating PDF from XML with servlet
    By Preethi in forum Advanced Java
    Replies: 2
    Last Post: 10-30-2008, 05:50 PM

Posting Permissions

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