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
2ea469a8
Commit
2ea469a8
authored
Aug 01, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
XPath waluty v3 sum / count
parent
a3504f9d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
0 deletions
+42
-0
WalutyXPath3.java
ObslugaXML/src/main/java/przyklady_xpath/WalutyXPath3.java
+42
-0
No files found.
ObslugaXML/src/main/java/przyklady_xpath/WalutyXPath3.java
0 → 100644
View file @
2ea469a8
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
WalutyXPath3
{
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"
);
String
ratesPath
=
"//Rate[Code = '"
+
szukanyKod
+
"']"
;
String
path
=
"sum("
+
ratesPath
+
"/Mid) div count("
+
ratesPath
+
")"
;
System
.
out
.
println
(
"Ewaluacja wyrażenia "
+
path
);
double
srednia
=
(
double
)
xpath
.
evaluate
(
path
,
doc
,
XPathConstants
.
NUMBER
);
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