Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
web-ui
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
WEBGDE-Components
web-ui
Commits
b487eb56
Commit
b487eb56
authored
Jan 08, 2024
by
Owen Ryan Ang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
logic changes when updating xml if existing.
parent
da4b5379
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
13 deletions
+38
-13
GDEWebServices.java
...a/com/svi/webgde/restservice/services/GDEWebServices.java
+2
-1
XMLUtil.java
...c/main/java/com/svi/webgde/restservice/utils/XMLUtil.java
+36
-12
No files found.
WebGde/src/main/java/com/svi/webgde/restservice/services/GDEWebServices.java
View file @
b487eb56
...
...
@@ -189,7 +189,8 @@ public class GDEWebServices {
if
(!
file
.
exists
())
{
response
=
XMLUtil
.
generateXML
(
xml
);
}
else
{
response
=
XMLUtil
.
udpateXML
(
xml
);
// response = XMLUtil.udpateXML(xml);
response
=
XMLUtil
.
generateXML
(
xml
);
}
}
catch
(
Exception
e
)
{
return
Response
.
status
(
500
).
entity
(
"Fail"
).
build
();
...
...
WebGde/src/main/java/com/svi/webgde/restservice/utils/XMLUtil.java
View file @
b487eb56
package
com
.
svi
.
webgde
.
restservice
.
utils
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.StringReader
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
...
...
@@ -108,8 +110,17 @@ public class XMLUtil {
transformer
.
setOutputProperty
(
OutputKeys
.
STANDALONE
,
"no"
);
transformer
.
setOutputProperty
(
"{http://xml.apache.org/xslt}indent-amount"
,
"4"
);
Result
result
=
new
StreamResult
(
xml
.
getOutputDir
());
try
(
OutputStream
outputStream
=
new
FileOutputStream
(
xml
.
getOutputDir
()))
{
// Set the output stream for the StreamResult
Result
result
=
new
StreamResult
(
outputStream
);
// Perform the transformation
transformer
.
transform
(
source
,
result
);
}
catch
(
IOException
e
)
{
// Handle IOException
e
.
printStackTrace
();
}
return
new
String
(
Files
.
readAllBytes
(
Paths
.
get
(
xml
.
getOutputDir
())));
}
...
...
@@ -158,23 +169,36 @@ public class XMLUtil {
Node
imagename
=
document
.
getElementsByTagName
(
"imagename"
).
item
(
imagenames
.
get
(
xml
.
getImageName
()));
Node
xmlSubRecord
=
imagename
.
getParentNode
().
getLastChild
().
getPreviousSibling
();
// xml.getFields().forEach((key, value) -> {
for
(
Map
.
Entry
<
String
,
String
>
entry
:
xml
.
getFields
().
entrySet
())
{
for
(
int
i
=
0
;
i
<
xmlSubRecord
.
getChildNodes
().
getLength
();
i
++)
{
String
key
=
entry
.
getKey
();
String
value
=
entry
.
getValue
();
if
(
xmlSubRecord
.
getChildNodes
().
item
(
i
).
getNodeType
()
==
Node
.
ELEMENT_NODE
)
{
if
(
xmlSubRecord
.
getChildNodes
().
item
(
i
).
getAttributes
().
getNamedItem
(
"no"
).
getNodeValue
()
.
equals
(
key
))
{
for
(
int
j
=
0
;
j
<
xmlSubRecord
.
getChildNodes
().
item
(
i
).
getChildNodes
().
getLength
();
j
++)
{
if
(
xmlSubRecord
.
getChildNodes
().
item
(
i
).
getChildNodes
().
item
(
j
)
.
getNodeType
()
==
Node
.
ELEMENT_NODE
)
{
xmlSubRecord
.
getChildNodes
().
item
(
i
).
getChildNodes
().
item
(
j
).
setTextContent
(
value
);
}
}
boolean
fieldExists
=
false
;
// Use XPath to find the field with the same "no" attribute
XPath
xPath
=
XPathFactory
.
newInstance
().
newXPath
();
String
expression
=
String
.
format
(
".//field[@no='%s']"
,
key
);
try
{
Node
existingField
=
(
Node
)
xPath
.
compile
(
expression
).
evaluate
(
xmlSubRecord
,
XPathConstants
.
NODE
);
if
(
existingField
!=
null
)
{
// Update the value of the existing field
existingField
.
getFirstChild
().
setTextContent
(
value
);
fieldExists
=
true
;
}
}
catch
(
XPathExpressionException
e
)
{
e
.
printStackTrace
();
// Handle the exception appropriately in your production code
}
// If the field does not exist, create and append a new one
if
(!
fieldExists
)
{
Element
xmlField
=
document
.
createElement
(
"field"
);
Element
valueField
=
document
.
createElement
(
"value"
);
valueField
.
appendChild
(
document
.
createTextNode
(
value
));
xmlField
.
setAttribute
(
"no"
,
key
);
xmlField
.
appendChild
(
valueField
);
xmlSubRecord
.
appendChild
(
xmlField
);
}
}
// });
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment