0

I have the following xml format:

<component>
   <observation classCode="OBS" moodCode="EVN">
      <value unit="mm[Hg]" value="120.0" xsi:type="PQ"/>
  </observation>
</component>

I want to change value "120.0" to "120". Thus, remove the decimal part using java code.

Currently, my code is the following but it's incomplete.

    NodeList nodePhysical = dom.getElementsByTagName("observation");

    for (int i = 0; i < nodePhysical.getLength(); i++) {
        Node node = nodePhysical.item(i);

        NodeList childNodes = node.getChildNodes();

    }
1
  • Look at the setAttribute function in org.w3c.dom.Element. Commented May 20, 2019 at 13:58

2 Answers 2

2

Try this,

    NodeList nodePhysical = dom.getElementsByTagName("observation");
    for (int i = 0; i < nodePhysical.getLength(); i++) {
    Node node = nodePhysical.item(i);
    NodeList childNodes = node.getChildNodes();
    Element ele;
    for (int count = 0; count < childNodes.getLength(); count++) {
    ele= (Element) childNodes.item(count);
    ele.setAttribute("value",ele.getAttribute("value").split("\\.")[0]);
    }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

What is nodeList? And where do I have this? Within the for-loop that I already have?
Thanks. Also, attribute "id" does not exist. Where do I have to locate this code? Within my other for-loop? Can you please write the full code, including what you need from my current code please?
0

Thank you @Srinivasan Sekar for your help!

I fixed it.

My new code is:

    NodeList nodePhysical = dom.getElementsByTagName("observation");

    for (int i = 0; i < nodePhysical.getLength(); i++) {
        Node node = nodePhysical.item(i);
        NodeList nodeC = node.getChildNodes();
        for (int j = 0; j < nodeC.getLength(); j++) {
            if(nodeC.item(j).getNodeType() == Node.ELEMENT_NODE){
                Element element = (Element) nodeC.item(j);
                element.setAttribute("value", (element.getAttribute("value").split("\\.")[0]));
            }
        }
    }

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.