Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jvstd1
Overview
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
Patryk Czarnik
jvstd1
Commits
d9e3c2d9
Commit
d9e3c2d9
authored
Oct 23, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
waluty - wersja DOM
parent
34cdfc92
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
0 deletions
+67
-0
Waluty1_Dom.java
src/main/java/p20_xml/Waluty1_Dom.java
+67
-0
No files found.
src/main/java/p20_xml/Waluty1_Dom.java
0 → 100644
View file @
d9e3c2d9
package
p20_xml
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Element
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
javax.xml.parsers.DocumentBuilder
;
import
javax.xml.parsers.DocumentBuilderFactory
;
import
java.io.File
;
public
class
Waluty1_Dom
{
public
static
void
main
(
String
[]
args
)
{
File
plik
=
new
File
(
"pliki/waluty2023.xml"
);
String
waluta
=
"EUR"
;
System
.
out
.
println
(
"Startujemy"
);
long
t1
=
System
.
nanoTime
();
try
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
DocumentBuilder
builder
=
dbf
.
newDocumentBuilder
();
Document
doc
=
builder
.
parse
(
plik
);
long
t2
=
System
.
nanoTime
();
double
min
=
Double
.
MAX_VALUE
,
max
=
0
,
sum
=
0
;
int
count
=
0
;
NodeList
tables
=
doc
.
getElementsByTagName
(
"ExchangeRatesTable"
);
final
int
n
=
tables
.
getLength
();
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
Element
table
=
(
Element
)
tables
.
item
(
i
);
// ryzyko NPE, gdyby plik był niepoprawny:
String
effectiveDate
=
table
.
getElementsByTagName
(
"EffectiveDate"
).
item
(
0
).
getTextContent
();
// System.out.println(effectiveDate);
NodeList
rates
=
table
.
getElementsByTagName
(
"Rate"
);
final
int
m
=
rates
.
getLength
();
for
(
int
j
=
0
;
j
<
m
;
j
++)
{
Element
rate
=
(
Element
)
rates
.
item
(
j
);
String
code
=
rate
.
getElementsByTagName
(
"Code"
).
item
(
0
).
getTextContent
();
if
(
code
.
equals
(
waluta
))
{
double
mid
=
Double
.
parseDouble
(
rate
.
getElementsByTagName
(
"Mid"
).
item
(
0
).
getTextContent
());
count
++;
sum
+=
mid
;
if
(
mid
<
min
)
min
=
mid
;
if
(
mid
>
max
)
max
=
mid
;
}
}
}
long
t3
=
System
.
nanoTime
();
Runtime
runtime
=
Runtime
.
getRuntime
();
long
pamiec
=
runtime
.
totalMemory
()
-
runtime
.
freeMemory
();
System
.
out
.
printf
(
"Czas czytania pliku: %.6f s%n"
,
(
t2
-
t1
)
*
1
e
-
9
);
System
.
out
.
printf
(
"Czas liczenia : %.6f s%n"
,
(
t3
-
t2
)
*
1
e
-
9
);
System
.
out
.
printf
(
"Czas łączny : %.6f s%n"
,
(
t3
-
t1
)
*
1
e
-
9
);
System
.
out
.
printf
(
"Zajęta pamięć : %,d B%n"
,
pamiec
);
System
.
out
.
println
(
"Count: "
+
count
);
if
(
count
>
0
)
{
System
.
out
.
println
(
"Avg: "
+
(
sum
/
count
));
System
.
out
.
println
(
"Min: "
+
min
);
System
.
out
.
println
(
"Max: "
+
max
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
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