1
<?xml version="1.0"?>
<AllConcepts>
    <Level id="1">
        <TST>RegisterPatient</TST>
    </Level>
    <Level id="2">
        <TST>PersonwithInpatientEncounter</TST> 
        <TST>InpatientwithDiagnoses</TST> 
        <TST>InpatientwithRadiologyOrder</TST>      
    </Level>
    <Level id="3">
        <TST>InpatientwithProblem</TST> 
        <TST>InpatientwithAllergy</TST>             
    </Level>
</AllConcepts>

Above is my XML. Please help me to get child node id by giving value in Java.

Example:

  • If i give value=InpatientwithDiagnoses, it should give me node 2;
  • If i give value = RegisterPatient, it should give me node 1.
1
  • 4
    Welcome to SO! What have you tried so far? Commented Sep 5, 2018 at 13:58

2 Answers 2

1

I would suggest using SAAJ!

It is a great Java library that gives us Java functionality to XML documents. You can use this library to construct, edit, or make SOAP messages. From there you can extract what is needed once that SOAP object is created.

Here is what you can use to create that java object:

// Use SAAJ to convert Document to SOAPElement
// Create SoapMessage
SOAPMessage message = createSOAPMessage();
SOAPBody soapBody = message.getSOAPBody();
// This returns the SOAPBodyElement
SOAPElement xml = soapBody.addDocument(doc);

And then something like this to get content from the XML body:

java.util.Iterator iterator = soapBody.getChildElements(bodyName);
SOAPBodyElement bodyElement = (SOAPBodyElement)iterator.next();
String node = bodyElement.getValue();

References:
https://docs.oracle.com/javase/7/docs/api/javax/xml/soap/SOAPMessage.html https://docs.oracle.com/javaee/5/tutorial/doc/bnbhr.html#bnbhz

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

4 Comments

what does SOAP have to do with this?
@SomeDude All SOAP messages contain an XML document. So one could use this library to construct a Java object from where ever one is receiving this XML document. So one can use this library to construct a java XML object from a local document or dynamically if he receives the XML document via a SOAP request/response. Just FYI this isnt a third party library it is in the Java SDK you just have to add the import like you would any other Java library
Using DOM cant we do this?
@SVM yea we can! Saaj just lets you do a little bit more if you need in the future to send or receive XML data using the SOAP protocol.
0

You could use java.xml.xpath - no need to install third party libs.

and use this xpath to get desired element given a TST value.

//TST[.='<value>']/parent::Level

where <value> could be RegisterPatient or InpatientwithDiagnoses or anything.

Refer to java's xpath documentation here.

Comments

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.