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
482b1c9a
Commit
482b1c9a
authored
Aug 02, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dodatkowy przykład SAAJ (weather)
parent
d621179e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
104 additions
and
0 deletions
+104
-0
GetWeather.java
SAAJ_Klient/src/main/java/saaj_klient/stare/GetWeather.java
+104
-0
No files found.
SAAJ_Klient/src/main/java/saaj_klient/stare/GetWeather.java
0 → 100644
View file @
482b1c9a
package
saaj_klient
.
stare
;
import
java.io.IOException
;
import
java.io.StringReader
;
import
javax.xml.namespace.QName
;
import
javax.xml.parsers.DocumentBuilder
;
import
javax.xml.parsers.DocumentBuilderFactory
;
import
javax.xml.parsers.ParserConfigurationException
;
import
jakarta.xml.soap.MessageFactory
;
import
jakarta.xml.soap.SOAPBody
;
import
jakarta.xml.soap.SOAPConnection
;
import
jakarta.xml.soap.SOAPConnectionFactory
;
import
jakarta.xml.soap.SOAPElement
;
import
jakarta.xml.soap.SOAPException
;
import
jakarta.xml.soap.SOAPFault
;
import
jakarta.xml.soap.SOAPMessage
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
org.xml.sax.InputSource
;
import
org.xml.sax.SAXException
;
public
class
GetWeather
{
public
static
final
String
WEBSERVICEX_NS
=
"http://www.webserviceX.NET"
;
private
static
void
prepareRequest
(
SOAPMessage
req
)
throws
SOAPException
{
final
SOAPBody
body
=
req
.
getSOAPBody
();
final
SOAPElement
get_weather
=
body
.
addChildElement
(
new
QName
(
WEBSERVICEX_NS
,
"GetWeather"
));
final
SOAPElement
city
=
get_weather
.
addChildElement
(
new
QName
(
WEBSERVICEX_NS
,
"cityName"
));
final
SOAPElement
country
=
get_weather
.
addChildElement
(
new
QName
(
WEBSERVICEX_NS
,
"CountryName"
));
city
.
setTextContent
(
"Warszawa"
);
country
.
setTextContent
(
"Poland"
);
// Niektóre serwisy wymagają ustawienia ngłówka HTTP SOAPAction.
// Implementacje realizowane w .NET tak właśnie mają. Jest to opisane w WSDL w części binding
req
.
getMimeHeaders
().
setHeader
(
"SOAPAction"
,
"http://www.webserviceX.NET/GetWeather"
);
}
private
static
void
processResponse
(
SOAPMessage
resp
)
throws
SOAPException
,
ParserConfigurationException
,
SAXException
,
IOException
{
SOAPBody
body
=
resp
.
getSOAPBody
();
NodeList
lista
=
body
.
getElementsByTagNameNS
(
WEBSERVICEX_NS
,
"GetWeatherResult"
);
if
(
lista
.
getLength
()
==
0
)
{
System
.
out
.
println
(
"Brak elementu GetWeatherResult"
);
return
;
}
Node
weather_result
=
lista
.
item
(
0
);
String
zagniezdzony_xml
=
weather_result
.
getTextContent
();
System
.
out
.
println
(
"\n======\nZagniezdzony XML:"
);
System
.
out
.
println
(
zagniezdzony_xml
);
DocumentBuilderFactory
bf
=
DocumentBuilderFactory
.
newInstance
();
DocumentBuilder
builder
=
bf
.
newDocumentBuilder
();
Document
doc
=
builder
.
parse
(
new
InputSource
(
new
StringReader
(
zagniezdzony_xml
)));
lista
=
doc
.
getElementsByTagName
(
"Temperature"
);
if
(
lista
.
getLength
()
==
0
)
{
System
.
out
.
println
(
"Brak elementu Temperature"
);
return
;
}
Node
temp
=
lista
.
item
(
0
);
System
.
out
.
println
(
"Temperatura: "
+
temp
.
getTextContent
());
}
private
static
void
processFault
(
SOAPFault
fault
)
{
System
.
out
.
println
(
"Blad:"
);
System
.
out
.
println
(
fault
.
getFaultString
());
}
public
static
void
main
(
String
[]
args
)
{
try
{
System
.
out
.
println
(
"Zapytanie:"
);
MessageFactory
mf
=
MessageFactory
.
newInstance
();
SOAPMessage
req
=
mf
.
createMessage
();
prepareRequest
(
req
);
req
.
writeTo
(
System
.
out
);
System
.
out
.
println
(
"\n=====================\n\nOdpowiedz:"
);
SOAPConnectionFactory
cf
=
SOAPConnectionFactory
.
newInstance
();
SOAPConnection
con
=
cf
.
createConnection
();
SOAPMessage
resp
=
con
.
call
(
req
,
"http://www.webservicex.net/globalweather.asmx"
);
resp
.
writeTo
(
System
.
out
);
System
.
out
.
println
();
if
(
resp
.
getSOAPBody
().
hasFault
())
{
processFault
(
resp
.
getSOAPBody
().
getFault
());
}
else
{
processResponse
(
resp
);
}
}
catch
(
SOAPException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
ParserConfigurationException
e
)
{
e
.
printStackTrace
();
}
catch
(
SAXException
e
)
{
e
.
printStackTrace
();
}
}
}
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