Skip to content

Instantly share code, notes, and snippets.

@annagapuz
Last active May 17, 2018 21:09
Show Gist options
  • Save annagapuz/f7f231338325cef57804518fe01fb69c to your computer and use it in GitHub Desktop.
Save annagapuz/f7f231338325cef57804518fe01fb69c to your computer and use it in GitHub Desktop.
/**
* fieldIdentifier = //ROOT/FOO[0], fieldValue = abc
* fieldIdentifier = //ROOT/FOO[1]/@Bar, fieldValue = xyz
*/
public class SimpleXmlGenerator {
public static String createXml(String xmlRootName, List<FieldEnumeration> inputFields) {
StringBuilder xmlString = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
buildXml(xmlString, buildXmlNodes(xmlRootName, inputFields));
return xmlString.toString();
}
public static String createXml(XmlNode xmlNode) {
StringBuilder xmlString = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
buildXml(xmlString, xmlNode);
return xmlString.toString();
}
private static XmlNode buildXmlNodes(String xmlRootName, List<FieldEnumeration> inputFields) {
XmlNode rootNode = new XmlNode(xmlRootName);
inputFields.forEach(p -> {
XmlNode previousNode = rootNode;
for (String node : p.getFieldIdentifier().replaceAll("//", "").split("\\/")) {
XmlNode childNode;
if (node.startsWith("@")) {
childNode = new XmlNode(node.replaceAll("@", ""), p.getFieldValue());
childNode.setAttribute(true);
childNode.setParent(previousNode.getName());
previousNode.addAttribute(childNode);
} else {
Optional<XmlNode> optionalChild = previousNode.findNode(node);
if (optionalChild.isPresent()) {
childNode = optionalChild.get();
} else {
childNode = new XmlNode(node);
previousNode.addChild(childNode);
}
}
previousNode = childNode;
}
if (!previousNode.isAttribute()) {
previousNode.setValue(p.getFieldValue());
}
});
return rootNode;
}
private static void buildXml(StringBuilder xmlString, XmlNode xmlNode) {
final String strippedNodeName = xmlNode.getName().replaceAll("\\[[\\d]\\]", "");
xmlString.append("<").append(strippedNodeName);
if (xmlNode.hasAttributes()) {
xmlNode.getAttributes().forEach(n -> xmlString.append(" ").append(n.getName()).append("=").append("\"").append(n.getValue()).append("\""));
}
if (xmlNode.hasChildren()) {
xmlString.append(">");
xmlNode.getChildren().forEach(c -> buildXml(xmlString, c));
xmlString.append("</").append(strippedNodeName).append(">");
} else {
if (Objects.nonNull(xmlNode.getValue())) {
xmlString.append(">").append(xmlNode.getValue()).append("</").append(strippedNodeName).append(">");
} else {
xmlString.append("/>");
}
}
}
}
public class XmlNode {
private String name;
private String parent;
private String value;
private boolean isAttribute;
private List<XmlNode> children;
private List<XmlNode> attributes;
public XmlNode(String name) {
this.name = name;
this.isAttribute = false;
this.children = new ArrayList<>();
this.attributes = new ArrayList<>();
}
public XmlNode(String name, String value) {
this(name);
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public boolean isAttribute() {
return isAttribute;
}
public void setAttribute(boolean attribute) {
isAttribute = attribute;
}
public List<XmlNode> getChildren() {
return children;
}
public void setChildren(List<XmlNode> children) {
this.children = children;
}
public List<XmlNode> getAttributes() {
return attributes;
}
public void setAttributes(List<XmlNode> attributes) {
this.attributes = attributes;
}
public void addChild(XmlNode childNode) {
this.children.add(childNode);
}
public void addAttribute(XmlNode attributeNode) {
this.attributes.add(attributeNode);
}
public Optional<XmlNode> findNode(String nodeName) {
if (nodeName.equals(this.getName())) {
return Optional.of(this);
}
return getChildren().stream().filter(n -> nodeName.equals(n.getName())).findFirst();
}
public boolean hasChildren() {
return !getChildren().isEmpty();
}
public boolean hasAttributes() {
return !getAttributes().isEmpty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment