If you are using Java 8, you should have a look at my unXml-library. It's open source and available on Maven Central.
This code would work for your project
import com.nerdforge.unxml.Parsing;
import com.nerdforge.unxml.factory.ParsingFactory;
import org.w3c.dom.Document;
import java.util.List;
public class Parser {
public ObjectNode parseXml(String xml){
Parsing parsing = ParsingFactory.getInstance().create();
Document document = parsing.xml().document(xml);
Parser<ObjectNode> parser = parsing.obj("Details")
.attribute("Records", parsing.obj("Records")
.attribute("name", parsing.obj("name")
.attribute("@id", "@id")
.attribute("#text", "text()")
)
.attribute("age", "age")
.attribute("gender", parsing.obj("gender")
.attribute("@type", "@type")
)
)
.build();
ObjectNode result = parser.apply(document);
return result;
}
}
Note that the whole point with this library, is to extract values from XMLs using Xpaths, and assigning them to attributes in Json Objects. So you can create "nicer" Json, that's not so bound to the xml structure.
Example
public class Parser {
public ArrayNode parseXml(String xml){
Parsing parsing = ParsingFactory.getInstance().create();
Document document = parsing.xml().document(xml);
Parser<ArrayNode> parser = parsing.arr("//Records", parsing.obj()
.attribute("id", "name/@id", parsing.number())
.attribute("name") // name is both the xpath, and json-attribute key
.attribute("age", "age", parsing.number())
.attribute("gender", "gender/@type")
).build();
ArrayNode result = parser.apply(document);
return result;
}
}
Will return the json
[{
id: 123,
name: "xyz",
age: 25,
gender: "male"
}]
Use parsing.arr("<xpath>", ...)
to create arrays.