Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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
javab_20230617
Commits
b9298c84
Commit
b9298c84
authored
Jul 16, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PobieranieXML - wersja statyczna
parent
7cd3cc68
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
130 deletions
+71
-130
PobieranieXML.java
.../src/main/java/com/example/demo/waluty/PobieranieXML.java
+68
-0
WalutyController.java
...c/main/java/com/example/demo/waluty/WalutyController.java
+1
-64
WalutyRest.java
...ing/src/main/java/com/example/demo/waluty/WalutyRest.java
+2
-66
No files found.
PC25-Spring/src/main/java/com/example/demo/waluty/PobieranieXML.java
0 → 100644
View file @
b9298c84
package
com
.
example
.
demo
.
waluty
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.net.URL
;
import
java.time.LocalDate
;
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
org.w3c.dom.Document
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
org.xml.sax.SAXException
;
public
class
PobieranieXML
{
public
static
TabelaWalut
pobierzTabele
(
String
data
)
{
String
adres
=
"https://api.nbp.pl/api/exchangerates/tables/a"
;
if
(
data
!=
null
)
{
adres
+=
"/"
+
data
;
}
adres
+=
"?format=xml"
;
try
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
DocumentBuilder
db
=
dbf
.
newDocumentBuilder
();
Document
doc
=
null
;
URL
url
=
new
URL
(
adres
);
try
(
InputStream
in
=
url
.
openStream
())
{
doc
=
db
.
parse
(
in
);
}
XPathFactory
xpf
=
XPathFactory
.
newInstance
();
XPath
xpath
=
xpf
.
newXPath
();
// String numer = xpath.evaluate(
// "/ArrayOfExchangeRatesTable/ExchangeRatesTable/No", doc);
String
nazwaTabeli
=
xpath
.
evaluate
(
"//Table"
,
doc
);
String
numerTabeli
=
xpath
.
evaluate
(
"//No"
,
doc
);
LocalDate
dataTabeli
=
LocalDate
.
parse
(
xpath
.
evaluate
(
"//EffectiveDate"
,
doc
));
TabelaWalut
tabela
=
new
TabelaWalut
(
nazwaTabeli
,
numerTabeli
,
dataTabeli
);
NodeList
rates
=
(
NodeList
)
xpath
.
evaluate
(
"//Rate"
,
doc
,
XPathConstants
.
NODESET
);
final
int
n
=
rates
.
getLength
();
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
Node
rate
=
rates
.
item
(
i
);
String
kod
=
xpath
.
evaluate
(
"Code"
,
rate
);
String
nazwa
=
xpath
.
evaluate
(
"Currency"
,
rate
);
BigDecimal
kurs
=
new
BigDecimal
(
xpath
.
evaluate
(
"Mid"
,
rate
));
Waluta
waluta
=
new
Waluta
(
kod
,
nazwa
,
kurs
);
tabela
.
dodajWalute
(
waluta
);
}
return
tabela
;
}
catch
(
ParserConfigurationException
|
SAXException
|
XPathExpressionException
|
IOException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
}
PC25-Spring/src/main/java/com/example/demo/waluty/WalutyController.java
View file @
b9298c84
package
com
.
example
.
demo
.
waluty
;
package
com
.
example
.
demo
.
waluty
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.net.URL
;
import
java.time.LocalDate
;
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
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
org.xml.sax.SAXException
;
@Controller
@Controller
public
class
WalutyController
{
public
class
WalutyController
{
@GetMapping
(
"/waluty"
)
@GetMapping
(
"/waluty"
)
public
String
wyswietlWaluty
(
String
data
,
Model
model
)
{
public
String
wyswietlWaluty
(
String
data
,
Model
model
)
{
TabelaWalut
tabela
=
pobierzTabele
(
data
);
TabelaWalut
tabela
=
PobieranieXML
.
pobierzTabele
(
null
);
model
.
addAttribute
(
"tabela"
,
tabela
);
model
.
addAttribute
(
"tabela"
,
tabela
);
return
"waluty.html"
;
return
"waluty.html"
;
}
}
private
TabelaWalut
pobierzTabele
(
String
data
)
{
String
adres
=
"https://api.nbp.pl/api/exchangerates/tables/a"
;
if
(
data
!=
null
)
{
adres
+=
"/"
+
data
;
}
adres
+=
"?format=xml"
;
try
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
DocumentBuilder
db
=
dbf
.
newDocumentBuilder
();
Document
doc
=
null
;
URL
url
=
new
URL
(
adres
);
try
(
InputStream
in
=
url
.
openStream
())
{
doc
=
db
.
parse
(
in
);
}
XPathFactory
xpf
=
XPathFactory
.
newInstance
();
XPath
xpath
=
xpf
.
newXPath
();
// String numer = xpath.evaluate(
// "/ArrayOfExchangeRatesTable/ExchangeRatesTable/No", doc);
String
nazwaTabeli
=
xpath
.
evaluate
(
"//Table"
,
doc
);
String
numerTabeli
=
xpath
.
evaluate
(
"//No"
,
doc
);
LocalDate
dataTabeli
=
LocalDate
.
parse
(
xpath
.
evaluate
(
"//EffectiveDate"
,
doc
));
TabelaWalut
tabela
=
new
TabelaWalut
(
nazwaTabeli
,
numerTabeli
,
dataTabeli
);
NodeList
rates
=
(
NodeList
)
xpath
.
evaluate
(
"//Rate"
,
doc
,
XPathConstants
.
NODESET
);
final
int
n
=
rates
.
getLength
();
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
Node
rate
=
rates
.
item
(
i
);
String
kod
=
xpath
.
evaluate
(
"Code"
,
rate
);
String
nazwa
=
xpath
.
evaluate
(
"Currency"
,
rate
);
BigDecimal
kurs
=
new
BigDecimal
(
xpath
.
evaluate
(
"Mid"
,
rate
));
Waluta
waluta
=
new
Waluta
(
kod
,
nazwa
,
kurs
);
tabela
.
dodajWalute
(
waluta
);
}
return
tabela
;
}
catch
(
ParserConfigurationException
|
SAXException
|
XPathExpressionException
|
IOException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
}
}
PC25-Spring/src/main/java/com/example/demo/waluty/WalutyRest.java
View file @
b9298c84
package
com
.
example
.
demo
.
waluty
;
package
com
.
example
.
demo
.
waluty
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.net.URL
;
import
java.time.LocalDate
;
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
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
org.xml.sax.SAXException
;
@RestController
@RestController
@RequestMapping
(
"/waluty.rest"
)
@RequestMapping
(
"/waluty.rest"
)
...
@@ -30,59 +11,14 @@ public class WalutyRest {
...
@@ -30,59 +11,14 @@ public class WalutyRest {
@GetMapping
@GetMapping
public
TabelaWalut
wyswietlWaluty
()
{
public
TabelaWalut
wyswietlWaluty
()
{
TabelaWalut
tabela
=
pobierzTabele
(
null
);
TabelaWalut
tabela
=
PobieranieXML
.
pobierzTabele
(
null
);
return
tabela
;
return
tabela
;
}
}
@GetMapping
(
"/{data}"
)
@GetMapping
(
"/{data}"
)
public
TabelaWalut
wyswietlWaluty
(
@PathVariable
String
data
)
{
public
TabelaWalut
wyswietlWaluty
(
@PathVariable
String
data
)
{
TabelaWalut
tabela
=
pobierzTabele
(
data
);
TabelaWalut
tabela
=
PobieranieXML
.
pobierzTabele
(
data
);
return
tabela
;
return
tabela
;
}
}
private
TabelaWalut
pobierzTabele
(
String
data
)
{
String
adres
=
"https://api.nbp.pl/api/exchangerates/tables/a"
;
if
(
data
!=
null
)
{
adres
+=
"/"
+
data
;
}
adres
+=
"?format=xml"
;
try
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
DocumentBuilder
db
=
dbf
.
newDocumentBuilder
();
Document
doc
=
null
;
URL
url
=
new
URL
(
adres
);
try
(
InputStream
in
=
url
.
openStream
())
{
doc
=
db
.
parse
(
in
);
}
XPathFactory
xpf
=
XPathFactory
.
newInstance
();
XPath
xpath
=
xpf
.
newXPath
();
// String numer = xpath.evaluate(
// "/ArrayOfExchangeRatesTable/ExchangeRatesTable/No", doc);
String
nazwaTabeli
=
xpath
.
evaluate
(
"//Table"
,
doc
);
String
numerTabeli
=
xpath
.
evaluate
(
"//No"
,
doc
);
LocalDate
dataTabeli
=
LocalDate
.
parse
(
xpath
.
evaluate
(
"//EffectiveDate"
,
doc
));
TabelaWalut
tabela
=
new
TabelaWalut
(
nazwaTabeli
,
numerTabeli
,
dataTabeli
);
NodeList
rates
=
(
NodeList
)
xpath
.
evaluate
(
"//Rate"
,
doc
,
XPathConstants
.
NODESET
);
final
int
n
=
rates
.
getLength
();
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
Node
rate
=
rates
.
item
(
i
);
String
kod
=
xpath
.
evaluate
(
"Code"
,
rate
);
String
nazwa
=
xpath
.
evaluate
(
"Currency"
,
rate
);
BigDecimal
kurs
=
new
BigDecimal
(
xpath
.
evaluate
(
"Mid"
,
rate
));
Waluta
waluta
=
new
Waluta
(
kod
,
nazwa
,
kurs
);
tabela
.
dodajWalute
(
waluta
);
}
return
tabela
;
}
catch
(
ParserConfigurationException
|
SAXException
|
XPathExpressionException
|
IOException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
}
}
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