Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Adding child nodes to xml reccursively using java

SoumikDec 28 2013 — edited Dec 29 2013

Hi,

I am having input XML as below:

<company name = "Company Name">

     <departments>

          <department id = "1" name = "Department 1 Name" childs = "true" />

         <department id = "2" name = "Department 2 Name" childs = "false" />

         <department id = "3" name = "Department 3 Name" childs = "true" />

     </departments>

</company>

And a Map containing the key as id of each department. Eg:

Key = "1"

Value =

<departments>

     <department id = "4" name = "Department 4 Name" childs = "yes" />

      <department id = "6" name = "Department 6 Name" childs = "yes" />

</departments>

Key = "4"

Value =

<departments>

     <department id = "5" name = "Department 5 Name" childs = "no" />

</departments>

Key = "6"

Value =

<departments>

    <department id = "7" name = "Department 7 Name" childs = "yes" />

    <department id = "14" name = "Department 14 Name" childs = "false" />

    <department id = "9" name = "Department 9 Name" childs = "true" />

</departments>

Key = "7"

Value =

<departments>

    <department id = "8" name = "Department 8 Name" childs = "yes" />

    <department id = "10" name = "Department 10 Name" childs = "no" />

</departments>

And so on

i.e. a List containing all the department id and corresponding sub departments

Expected Response is:

<company name = "Company Name">

    <departments>

        <department id = "1" name = "Department 1 Name" childs = "yes" >

            <departments>

                <department id = "4" name = "Department 4 Name" childs = "yes" >

                    <departments>

                        <department id = "5" name = "Department 5 Name" childs = "no" />

                    </departments>

                </department>

                <department id = "6" name = "Department 6 Name" childs = "yes" >

                    <departments>

                        <department id = "7" name = "Department 7 Name" childs = "yes" >

                            <departments>

                                <department id = "8" name = "Department 8 Name" childs = "yes" >

                                    <departments>

                                        <department id="12" name="Department 12 Name" childs="no"/>

                                        <department id="13" name="Department 13 Name" childs="no"/>

                                    </departments>

                                </department>

                                <department id = "10" name = "Department 10 Name" childs = "no" />

                            </departments>

                        </department>

                        <department id = "14" name = "Department 14 Name" childs = "false" />

                        <department id = "9" name = "Department 9 Name" childs = "true" >

                            <departments>

                                <department id = "15" name = "Department 15 Name" childs = "false" />

                            </departments>

                        </department>

                    </departments>

                </department>

            </departments>

        </department>

        <department id = "2" name = "Department 2 Name" childs = "false" />

        <department id = "3" name = "Department 3 Name" childs = "true" >

            <departments>

                <department id = "11" name = "Department 11 Name" childs = "false" />

            </departments>

        </department>

    </departments>

</company>

Can someone please help in achieving the same using java

Below is the java code used:

public static Node getReferences(Map<String, Node> resources, String key) {

Node result = null;

try {

          XPath xPath = XPathFactory.newInstance().newXPath();

          XPathExpression xPathExpression = xPath.compile("//department[@childs='yes']");

          Node n1 = resources.get(key);

          if (n1.getNodeType() == Node.ELEMENT_NODE &&

                n1.getNodeName().equalsIgnoreCase("departments")) {

               NodeList nL1 = (NodeList)xPathExpression.evaluate(n1, XPathConstants.NODESET);

               if (nL1.getLength() > 0) {

                    for (int i = 0; i < nL1.getLength(); i++) {

                         Node n2 = nL1.item(i);

                         Document document1 = n2.getOwnerDocument();

                        Element e1 = (Element)n2;

                         if (e1.getAttribute("childs").equalsIgnoreCase("yes")) {

                              String serviceName = e1.getAttribute("id");

                              Node n3 = resources.get(serviceName);

                            Node n4 = getReferences(resources, serviceName);

                              n3 = document1.importNode(n3, true);

                              Node appendChild = n2.appendChild(n3);

                         }

                    }

               } else {

                    result = n1;

               }

          }

} catch (XPathExpressionException e) {

            e.printStackTrace();

     } catch (Exception e) {

            e.printStackTrace();

     }

return result;

}

Comments

Naveent_2785

The below is working:

use JSON.parse() on the string and stored the output in a JSON object. Send the JSON object as a request to the REST reference

mateo91

Thanks! This helps!

Demetrius Brackens

Was javascript used to implement this solution? Do you have an example? Thanks.

mateo91

You must use the Javascript component after your transform component. Then you just add the JSON.parse() call on your JSON Object variable used in the Transform (e.g. JSON.parse(<jsonObjectVariable>);)
I ended up not even needing my Transform Component since I was receiving JSON in my request. Then I just used my Javascript component to parse what I needed in the receive payload before sending it off to the external destination.

Demetrius Brackens

Thanks for your help. This is working for me now.

Hi, I am having the same parsing issue. Can you show me example of JSON.parse(<jsonObjectVariable>);

mateo91

@User_489HG - Did you get this resolved? Let me know and I can provide an example. You can do this with both a JavaScript component and an Assign Component.

Maruthi Gottumukkala

Can you please provide me examples.

Maruthi Gottumukkala

Finally I figured it out. Appreciated for the tip. Thank you so much guys.

User_FRYTH

I am having the same issue kind of issue, in OSB 12c, when using xslt and nXSDTraslate, it shows output in binary format,
Then I have used commons-codec-1.7.jar to extract the string from binary format.
Now my rest api is not accepting the request, so I need to covert it from string to json then send it to REST api.
mateo91/User_489HG , Can you please share some example, how to do that.

1552110

After you Convert your XML to String using Translate activity, Add a JavaScript activity and add $JsonInput = JSON.parse($jsonInputString) to the Code snippet to convert the String to JSON object. In the example here $JsonInput vairiable is a JSON object input for my REST service call. Hope this helps.

User_FYU9P

Thanks for help. It is working

1 - 12
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jan 25 2014
Added on Dec 28 2013
0 comments
3,041 views