Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_20230801
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
alx_20230801
Commits
6893df37
Commit
6893df37
authored
Aug 01, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
XPath waluty v2 z ifem
parent
87dd1652
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
0 deletions
+52
-0
WalutyXPath2.java
ObslugaXML/src/main/java/przyklady_xpath/WalutyXPath2.java
+52
-0
No files found.
ObslugaXML/src/main/java/przyklady_xpath/WalutyXPath2.java
0 → 100644
View file @
6893df37
package
przyklady_xpath
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
org.xml.sax.SAXException
;
import
javax.xml.parsers.DocumentBuilder
;
import
javax.xml.parsers.DocumentBuilderFactory
;
import
javax.xml.parsers.ParserConfigurationException
;
import
javax.xml.xpath.XPath
;
import
javax.xml.xpath.XPathConstants
;
import
javax.xml.xpath.XPathExpressionException
;
import
javax.xml.xpath.XPathFactory
;
import
java.io.IOException
;
public
class
WalutyXPath2
{
public
static
void
main
(
String
[]
args
)
{
String
szukanyKod
=
"USD"
;
// chcemy obliczyć średni kurs podanej waluty z całego pliku
System
.
out
.
println
(
"Konfiguracja"
);
DocumentBuilderFactory
factory
=
DocumentBuilderFactory
.
newInstance
();
XPathFactory
xpf
=
XPathFactory
.
newInstance
();
try
{
XPath
xpath
=
xpf
.
newXPath
();
DocumentBuilder
builder
=
factory
.
newDocumentBuilder
();
System
.
out
.
println
(
"Czytanie pliku"
);
Document
doc
=
builder
.
parse
(
"waluty_2022.xml"
);
System
.
out
.
println
(
"Ewaluacja"
);
double
suma
=
0
;
int
ilosc
=
0
;
NodeList
rates
=
(
NodeList
)
xpath
.
evaluate
(
"//Rate"
,
doc
,
XPathConstants
.
NODESET
);
final
int
nRates
=
rates
.
getLength
();
for
(
int
i
=
0
;
i
<
nRates
;
i
++)
{
Node
rate
=
rates
.
item
(
i
);
String
code
=
xpath
.
evaluate
(
"Code"
,
rate
);
if
(
code
.
equalsIgnoreCase
(
szukanyKod
))
{
suma
+=
(
Double
)
xpath
.
evaluate
(
"Mid"
,
rate
,
XPathConstants
.
NUMBER
);
ilosc
++;
System
.
out
.
println
(
ilosc
);
}
}
double
srednia
=
suma
/
ilosc
;
System
.
out
.
println
(
"Średni kurs waluty "
+
szukanyKod
+
" = "
+
srednia
);
}
catch
(
ParserConfigurationException
|
SAXException
|
IOException
|
XPathExpressionException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
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