2

I am working on XML files using DOM parser and I am copying entire data from one xml file into another xml file with changes in values of some nodes.

My XML file looks like below:

 <ems:DeterminationRequest>
    <ems:MessageInformation>
    .........
    </ems:MessageInformation>
    <ems:case>
       <ns17:CalHEERSCaseInfo> ... </ns17:CalHEERSCaseInfo>
       <ns17:persons> ..... </ns17:persons>
    </ems:case>
 </ems:DeterminationRequest>

Now I want to add child node to 'ems:DeterminationRequest' which has other child nodes.

Links reffered for adding new nodes: link1,link2.

I know how to add child node to the parent node using following syntax:

....
Document doc = db.parse(new FileInputStream(new File("in.xml")));
Element element = doc.getDocumentElement();
Node node = doc.createElement("ns17:CaseReferals");
element.appendChild(node);
.....

But I want to add following syntax of node:

 <ns17:CaseReferrals >
        <ns17:OtherProgramInformationRequest>
            <ns17:CHDPInfoInd></ns17:CHDPInfoInd>
            <ns17:WICInfoInd></ns17:WICInfoInd>
            <ns17:FamilyPACTInfoInd></ns17:FamilyPACTInfoInd>
            <ns17:SHOPInfoInd></ns17:SHOPInfoInd>
            <ns17:EPSDTInfoInd></ns17:EPSDTInfoInd>
            <ns17:VoterRegistrationInfoInd></ns17:VoterRegistrationInfoInd>
            <ns17:PCSPInfoInd></ns17:PCSPInfoInd>
        </ns17:OtherProgramInformationRequest>
 </ns17:CaseReferrals>

So is there any way by which we can create a string of above syntax and ad as a child node to the parent node?

1 Answer 1

3

One element at a time.

Document doc = db.parse(new FileInputStream(new File("in.xml")));
Element documentElement= doc.getDocumentElement();
Node ns17CaseReferals= doc.createElement("ns17:CaseReferals");
documentElement.appendChild(ns17CaseReferals);
Node ns17CHDPInfoInd = doc.createElement("ns17:CHDPInfoInd");
ns17CaseReferals.appendChild(ns17CHDPInfoInd);

and so on (refactoring opportunities for the repetitive operation of adding an empty child element with a given name are likely).

Otherwise, just parse the text as a XML document to get DOM nodes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response :) . Is it possible to create a list of nodes and append as a child for "<ns17:OtherProgramInformationRequest>"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.