0

I am trying to read XML into a JSONNode so I can pull values from it. For some reason the .get() function is reading the wrong Node. This is the XML I am trying to read:

<order>
    <order_ID>Test123456789</transaction_id>
    <original_inception_date>2024-09-25</original_inception_date>
    <transaction>
        <effective_date>2023-12-31 00:00:00.000</effective_date>
        <transaction_id>1a123456a1234a12345678ab12a1a12a</transaction_id>
        <product>123</product>
        <company>1</company>
        <lob>LIFE</lob>
    </transaction>
</order>

And this is the Java code I am using to read it:

XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree(xmlString);

for(JsonNode forNode : node.get("transaction")) {
    policyID                    = node.get("order_id").asText();
    originalInceptionDate       = node.path("original_inception_date").asText();
    transactionEffectiveDate    = forNode.get("effective_date").asText();
    policyTransactionID         = forNode.get("transaction_id").asText();;
    product                     = forNode.get("product").asText();
    company                     = forNode.get("company").asText();
    lob                         = forNode.get("lob").asText();

}

Now for some reason when it gets to the for loop, instead of putting the transaction node and all its sub nodes into forNode, it instead just pulls the effective_date value and that's it. This causes an error to occur when it tries to read one of the SubNodes in the forNode. I am not sure why this is happening.

3
  • The XML in the question is invalid: <order_ID>Test123456789</transaction_id>. Also, there is no closing </order> tag. The question is also missing info about the specific libraries (and dependencies) being used - but I assume it's Jackson. Perhaps you can create a cleaned-up and runnable minimal reproducible example? Commented Aug 15 at 21:44
  • (Also, The Java "order_id" does not match the XML <order_ID>). You should show us the full error message and stack trace. If you have only one <transaction> in your file, then you cannot iterate it the way you are trying. See the answer provided by @pavelpijevschi. Commented Aug 15 at 22:16
  • @andrewJames The missing </order> was due to incorrect code formatting by the OP. Commented Aug 16 at 13:32

1 Answer 1

1

Your <transaction> is being parsed as an object, not an array, so your for loop iterates over its fields (effective_date, etc.) instead of over transactions. Check isArray() first and wrap it into an array if needed:

JsonNode txNode = root.path("transaction");
if (!txNode.isArray()) {
    txNode = new ObjectMapper().createArrayNode().add(txNode);
}

for (JsonNode txn : txNode) {
    System.out.println(txn.path("product").asText());
}
Sign up to request clarification or add additional context in comments.

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.