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
87dd1652
Commit
87dd1652
authored
Aug 01, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
XPath waluty v1
parent
af7999d6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
2 deletions
+56
-2
DomSimplePrinter.java
ObslugaXML/src/main/java/przyklady_dom/DomSimplePrinter.java
+1
-1
PrzykladDomBuilder.java
...gaXML/src/main/java/przyklady_dom/PrzykladDomBuilder.java
+1
-1
WalutyXPath1.java
ObslugaXML/src/main/java/przyklady_xpath/WalutyXPath1.java
+54
-0
No files found.
ObslugaXML/src/main/java/przyklady_dom/DomSimplePrinter.java
View file @
87dd1652
...
@@ -23,7 +23,7 @@ public class DomSimplePrinter {
...
@@ -23,7 +23,7 @@ public class DomSimplePrinter {
printChildren
(
nd
);
printChildren
(
nd
);
break
;
break
;
case
Node
.
ELEMENT_NODE
:
case
Node
.
ELEMENT_NODE
:
System
.
out
.
println
(
"ELEMENT: qName="
+
nd
.
getNodeName
()+
" URI=
"
+
nd
.
getNamespaceURI
()+
System
.
out
.
println
(
"ELEMENT: qName="
+
nd
.
getNodeName
()+
" URI="
+
nd
.
getNamespaceURI
()+
" localName="
+
nd
.
getLocalName
()+
" attributes:"
+
nd
.
getAttributes
().
getLength
());
" localName="
+
nd
.
getLocalName
()+
" attributes:"
+
nd
.
getAttributes
().
getLength
());
printChildren
(
nd
);
printChildren
(
nd
);
break
;
break
;
...
...
ObslugaXML/src/main/java/przyklady_dom/PrzykladDomBuilder.java
View file @
87dd1652
...
@@ -16,7 +16,7 @@ public class PrzykladDomBuilder {
...
@@ -16,7 +16,7 @@ public class PrzykladDomBuilder {
String
plik
=
"simple.xml"
;
String
plik
=
"simple.xml"
;
try
{
try
{
System
.
out
.
println
(
"Pocztek dzialania."
);
System
.
out
.
println
(
"Pocz
ą
tek dzialania."
);
/* tworzymy parser DOM */
/* tworzymy parser DOM */
DocumentBuilderFactory
factory
=
DocumentBuilderFactory
.
newInstance
();
DocumentBuilderFactory
factory
=
DocumentBuilderFactory
.
newInstance
();
...
...
ObslugaXML/src/main/java/przyklady_xpath/WalutyXPath1.java
0 → 100644
View file @
87dd1652
package
przyklady_xpath
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Element
;
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
WalutyXPath1
{
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
(
"/ArrayOfExchangeRatesTable/ExchangeRatesTable/Rates/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
))
{
double
mid
=
Double
.
parseDouble
(
xpath
.
evaluate
(
"Mid"
,
rate
));
suma
+=
mid
;
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