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.
<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?<transaction>in your file, then you cannot iterate it the way you are trying. See the answer provided by @pavelpijevschi.</order>was due to incorrect code formatting by the OP.