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
ba693fc6
Commit
ba693fc6
authored
Aug 01, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Analiaza Walut DOM - wersja 1 (getElementsByTagName)
parent
e6d01bd5
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
0 deletions
+53
-0
AnalizaWalut1.java
ObslugaXML/src/main/java/przyklady_dom/AnalizaWalut1.java
+53
-0
No files found.
ObslugaXML/src/main/java/przyklady_dom/AnalizaWalut1.java
0 → 100644
View file @
ba693fc6
package
przyklady_dom
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Element
;
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
java.io.IOException
;
public
class
AnalizaWalut1
{
public
static
void
main
(
String
[]
args
)
{
String
szukanyKod
=
"USD"
;
// chcemy obliczyć średni kurs podanej waluty z całego pliku
DocumentBuilderFactory
factory
=
DocumentBuilderFactory
.
newInstance
();
try
{
DocumentBuilder
builder
=
factory
.
newDocumentBuilder
();
System
.
out
.
println
(
"Mam DocumentBuilder: "
+
builder
);
Document
doc
=
builder
.
parse
(
"waluty_2022.xml"
);
System
.
out
.
println
(
"Dokument wczytany: "
+
doc
);
Element
root
=
doc
.
getDocumentElement
();
System
.
out
.
println
(
"Nazwa elememtu głównego: "
+
root
.
getTagName
());
double
suma
=
0
;
int
ilosc
=
0
;
NodeList
rates
=
root
.
getElementsByTagName
(
"Rate"
);
final
int
nRates
=
rates
.
getLength
();
for
(
int
i
=
0
;
i
<
nRates
;
i
++)
{
Element
rate
=
(
Element
)
rates
.
item
(
i
);
// zakładamy, że każdy Rate na pewno posiada podelement Code; gdyby nie miał, to będzie wyjątek
Element
elementCode
=
(
Element
)
rate
.
getElementsByTagName
(
"Code"
).
item
(
0
);
String
code
=
elementCode
.
getTextContent
();
if
(
code
.
equalsIgnoreCase
(
szukanyKod
))
{
Element
elementMid
=
(
Element
)
rate
.
getElementsByTagName
(
"Mid"
).
item
(
0
);
double
mid
=
Double
.
parseDouble
(
elementMid
.
getTextContent
());
suma
+=
mid
;
ilosc
++;
}
}
double
srednia
=
suma
/
ilosc
;
System
.
out
.
println
(
"Średni kurs waluty "
+
szukanyKod
+
" = "
+
srednia
);
}
catch
(
ParserConfigurationException
|
SAXException
|
IOException
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