I have a use case where I have to parse an XML document and fill details in a corresponding fields of an internal model POJO Details
. Let's say, the XML look like following -
<?xml version="1.0" encoding="UTF-8"?>
...
<TagA>
<Child1>
...
</Child1>
<Child2>
...
</Child2>
</TagA>
...
<TagB>
<Child1>
...
</Child1>
<Child2>
...
</Child2>
</TagB>
....
And the internal model POJO looks like following -
public class Details {
...
Set<TagAInfo> tagAInformation;
Set<TagBInfo> tagBInformation;
...
}
The XML has multiple fields like TagAs, TagBs etc.
Current implementation: So currently there is a mapper/parser class(let's say Mapper.java
) that calls multiple methods like mapTagAInfo(details, xmlRootElement)
, mapTagBInfo(details, xmlRootElement)
etc. on details
(i.e. instance of Details.java
) something like following -
public class Mapper {
....
public Details mapInfo(XmlElement xmlRootElement) {
Details details = new Details();
mapTagAInfo(details, xmlRootElement)
mapTagBInfo(details, xmlRootElement)
mapTagCInfo(details, xmlRootElement)
....
return details;
}
private void mapTagAInfo(details, xmlRootElement) {
stp1: Extract <TagA> tag element info using a utility which reads the xml document
stp2: use the stp1 info and convert to internal model POJO TagAInfo and
add to details (details.addTagAInfo(tagAInfo))
}
Question: The current implementation makes the code look very ugly(as in a single class Mapper.java
multiple things are happening), so wanted to know if I can use some design pattern to improve the code ? If so, please suggest what design pattern to use and an example explaining the design pattern usage would help a lot.
Aucun commentaire:
Enregistrer un commentaire