Commit 3e4bdca3 by Owen Ryan Ang

XML parsing logic update

parent a7b591e6
...@@ -18,7 +18,12 @@ export async function WriteForm(e,metrics,doctype,section) { ...@@ -18,7 +18,12 @@ export async function WriteForm(e,metrics,doctype,section) {
for (var i=0;i<Nodes.length;i++) { for (var i=0;i<Nodes.length;i++) {
if (Nodes[i].style.display === 'none' && !Nodes[i].classList.contains('hidden')) continue if (Nodes[i].style.display === 'none' && !Nodes[i].classList.contains('hidden')) continue
let fid = Nodes[i].id; let fid = Nodes[i].id;
if (fid == 'DocType' || fid == 'Section' || fid == '' || fid == "submitButton") continue // Add doctype as field1
if (fid === 'DocType') {
fields['1'] = Nodes[i].value;
continue;
}
if (fid == 'Section' || fid == '' || fid == "submitButton") continue
// Skip the textbox in checklist and radiolist // Skip the textbox in checklist and radiolist
if (Nodes[i].classList.contains('radioOther') || Nodes[i].classList.contains('checkboxOther')) continue; if (Nodes[i].classList.contains('radioOther') || Nodes[i].classList.contains('checkboxOther')) continue;
// Skip elements of type "button", "submit", and files // Skip elements of type "button", "submit", and files
......
...@@ -2,5 +2,5 @@ export const PROJECT_CODE = "PROJCODE01"; ...@@ -2,5 +2,5 @@ export const PROJECT_CODE = "PROJCODE01";
export const ENCODING_PASS = "PASS1"; export const ENCODING_PASS = "PASS1";
export const GFS_URL = "http://107.20.193.188/gfs-explorer-ws/svc/gfs-rest/"; export const GFS_URL = "http://107.20.193.188/gfs-explorer-ws/svc/gfs-rest/";
export const TEMPORARY_FOLDER = "E:/Coding/Mobile GDE Elements"; export const TEMPORARY_FOLDER = "C:/Users/oang/Desktop/Mobile GDE/Elements";
export const GFS_ROOT_FOLDER = "/Users"; export const GFS_ROOT_FOLDER = "/Users";
\ No newline at end of file
...@@ -41,7 +41,16 @@ public class SubRecord { ...@@ -41,7 +41,16 @@ public class SubRecord {
*/ */
public void arrangeFields(String orderPipeDelimited) { public void arrangeFields(String orderPipeDelimited) {
List<Field> sortedFields = fields.stream() List<Field> sortedFields = fields.stream()
.sorted(Comparator.comparingInt(f -> getOrder(f.getNo(), orderPipeDelimited))) .sorted((f1, f2) -> {
if ("1".equals(f1.getNo())) {
return -1; // f1 comes before f2
} else if ("1".equals(f2.getNo())) {
return 1; // f2 comes before f1
} else {
return Integer.compare(getOrder(f1.getNo(), orderPipeDelimited),
getOrder(f2.getNo(), orderPipeDelimited));
}
})
.collect(Collectors.toList()); .collect(Collectors.toList());
// Update the fields list with the sorted order // Update the fields list with the sorted order
......
...@@ -81,10 +81,11 @@ public class XMLUtil { ...@@ -81,10 +81,11 @@ public class XMLUtil {
OutputXML output = new OutputXML(header, record); OutputXML output = new OutputXML(header, record);
//SAXParserFactory spf = SAXParserFactory.newInstance(); // SAXParserFactory spf = SAXParserFactory.newInstance();
//SAXParser sp = spf.newSAXParser(); // SAXParser sp = spf.newSAXParser();
//Source source = new SAXSource(sp.getXMLReader(), // Source source = new SAXSource(sp.getXMLReader(),
//new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[ ](?![^\\W_])", " ")))); // new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[
// ](?![^\\W_])", " "))));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = factory.newDocumentBuilder();
...@@ -348,26 +349,28 @@ public class XMLUtil { ...@@ -348,26 +349,28 @@ public class XMLUtil {
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xml.getOutputDir()); Document document = builder.parse(xml.getOutputDir());
Node imagename = document.getElementsByTagName("imagename").item(0); Element record = (Element) document.getElementsByTagName("record").item(0);
Node xmlSubRecord = imagename.getParentNode().getLastChild().getPreviousSibling(); if (record != null) {
Map<String, String> fields = new HashMap<>(); records.put("DocType", getElementTextByTagName(record, "doctype"));
records.put("Section", getElementTextByTagName(record, "section"));
records.put("DocType", imagename.getParentNode().getFirstChild().getNextSibling().getNextSibling() Map<String, String> fields = new HashMap<>();
.getNextSibling().getTextContent());
records.put("Section", imagename.getParentNode().getFirstChild().getNextSibling().getNextSibling()
.getNextSibling().getNextSibling().getNextSibling().getTextContent());
records.put("fields", fields); records.put("fields", fields);
for (int i = 0; i < xmlSubRecord.getChildNodes().getLength(); i++) { Element subRecord = (Element) record.getElementsByTagName("sub-record").item(0);
if (xmlSubRecord.getChildNodes().item(i).getNodeType() == Node.ELEMENT_NODE) { if (subRecord != null) {
for (int j = 0; j < xmlSubRecord.getChildNodes().item(i).getChildNodes().getLength(); j++) { NodeList fieldList = subRecord.getElementsByTagName("field");
if (xmlSubRecord.getChildNodes().item(i).getChildNodes().item(j) for (int j = 0; j < fieldList.getLength(); j++) {
.getNodeType() == Node.ELEMENT_NODE) { Element field = (Element) fieldList.item(j);
fields.put( String noAttributeValue = field.getAttribute("no");
xmlSubRecord.getChildNodes().item(i).getAttributes().getNamedItem("no").getNodeValue(), String textContentValue = field.getElementsByTagName("value").item(0).getTextContent();
xmlSubRecord.getChildNodes().item(i).getChildNodes().item(j).getTextContent());
records.put("fields", fields); if ("1".equals(noAttributeValue)) {
// Replace the value of "DocType" with the text content
records.put("DocType", textContentValue);
} }
fields.put(noAttributeValue, textContentValue);
} }
} }
} }
...@@ -375,4 +378,12 @@ public class XMLUtil { ...@@ -375,4 +378,12 @@ public class XMLUtil {
return records; return records;
} }
private static String getElementTextByTagName(Element parentElement, String tagName) {
NodeList elements = parentElement.getElementsByTagName(tagName);
if (elements.getLength() > 0) {
return elements.item(0).getTextContent();
}
return null; // or an appropriate default value
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment