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
a76b8e69
Commit
a76b8e69
authored
Oct 23, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
waluty - wersja XPath - początek
parent
d9e3c2d9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
1 deletions
+74
-1
Waluty1_Dom.java
src/main/java/p20_xml/Waluty1_Dom.java
+0
-1
Waluty2_XPath.java
src/main/java/p20_xml/Waluty2_XPath.java
+74
-0
No files found.
src/main/java/p20_xml/Waluty1_Dom.java
View file @
a76b8e69
...
@@ -2,7 +2,6 @@ package p20_xml;
...
@@ -2,7 +2,6 @@ package p20_xml;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Element
;
import
org.w3c.dom.Element
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
org.w3c.dom.NodeList
;
import
javax.xml.parsers.DocumentBuilder
;
import
javax.xml.parsers.DocumentBuilder
;
...
...
src/main/java/p20_xml/Waluty2_XPath.java
0 → 100644
View file @
a76b8e69
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
javax.xml.xpath.XPath
;
import
javax.xml.xpath.XPathConstants
;
import
javax.xml.xpath.XPathFactory
;
import
java.io.File
;
public
class
Waluty2_XPath
{
// Wersja w swojej strukturze podobna do Waluty1_Dom, ale zamiast getElementsByTagName będziemy tu używać wyrażeń XPath
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
{
XPathFactory
xpf
=
XPathFactory
.
newInstance
();
XPath
xpath
=
xpf
.
newXPath
();
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
=
(
NodeList
)
xpath
.
evaluate
(
"//ExchangeRatesTable"
,
doc
,
XPathConstants
.
NODESET
);
final
int
n
=
tables
.
getLength
();
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
Node
table
=
tables
.
item
(
i
);
String
effectiveDate
=
xpath
.
evaluate
(
"EffectiveDate"
,
table
);
System
.
out
.
println
(
effectiveDate
);
NodeList
rates
=
(
NodeList
)
xpath
.
evaluate
(
"Rates/Rate"
,
table
,
XPathConstants
.
NODESET
);
final
int
m
=
rates
.
getLength
();
for
(
int
j
=
0
;
j
<
m
;
j
++)
{
Element
rate
=
(
Element
)
rates
.
item
(
j
);
String
code
=
xpath
.
evaluate
(
"Code"
,
rate
);
if
(
code
.
equals
(
waluta
))
{
double
mid
=
(
Double
)
xpath
.
evaluate
(
"Mid"
,
rate
,
XPathConstants
.
NUMBER
);
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