Java method for accessing XML data returned by WebService

Source: Internet
Author: User

Java method for accessing XML data returned by WebService

This example describes how Java accesses WebService to return XML data. Share it with you for your reference. The details are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

Import java. io. IOException;

Import java. io. InputStream;

Import java.net. MalformedURLException;

Import java.net. URL;

Import java.net. URLConnection;

Import java. io. FileNotFoundException;

Import java. io. FileOutputStream;

Import java. io. PrintWriter;

Import org. w3c. dom. Document;

Import org. w3c. dom. DOMException;

Import org. xml. sax. SAXException;

Import javax. xml. parsers. DocumentBuilder;

Import javax. xml. parsers. DocumentBuilderFactory;

Import javax. xml. parsers. ParserConfigurationException;

Import javax. xml. transform. OutputKeys;

Import javax. xml. transform. Transformer;

Import javax. xml. transform. TransformerConfigurationException;

Import javax. xml. transform. TransformerException;

Import javax. xml. transform. TransformerFactory;

Import javax. xml. transform. dom. DOMSource;

Import javax. xml. transform. stream. StreamResult;

/***

* @ Author xuechong

* 6/11/2010 16: 58

* DomXMLString. java

* Overview: access the xml format data returned by the remote WebService Interface in java mode is saved locally.

*/

Public class DomXMLString {

Private static String SERVICES_HOST = "www.webxml.com.cn ";

// Remote WebService Interface url

Private static String NETDATA_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince ";

// Access the absolute path of the xml format data returned by the remote WebService Interface stored locally

Private static String LOCAL_PC_SAVEFILE_URL = "E: dataTest/netDataToLocalFile. xml ";

Private DomXMLString (){}

Public static void main (String [] args) throws Exception {

Document document = getProvinceCode (NETDATA_URL );

HelloOK (document, LOCAL_PC_SAVEFILE_URL );

}

/* Return a Document Object */

Public static Document getProvinceCode (String netXMLDataURL ){

Document document = null;

DocumentBuilderFactory documentBF = DocumentBuilderFactory. newInstance ();

DocumentBF. setNamespaceAware (true );

Try {

DocumentBuilder documentB = documentBF. newDocumentBuilder ();

InputStream inputStream = getSoapInputStream (netXMLDataURL );

// Related to the specific webService

Document = documentB. parse (inputStream );

InputStream. close ();

} Catch (DOMException e ){

E. printStackTrace ();

Return null;

} Catch (ParserConfigurationException e ){

E. printStackTrace ();

Return null;

} Catch (SAXException e ){

E. printStackTrace ();

Return null;

} Catch (IOException e ){

E. printStackTrace ();

Return null;

}

Return document;

}

/* Return the InputStream object */

Public static InputStream getSoapInputStream (String url ){

InputStream inputStream = null;

Try {

URL urlObj = new URL (url );

URLConnection urlConn = urlObj. openConnection ();

UrlConn. setRequestProperty ("Host", SERVICES_HOST );

// Related to the specific webService

UrlConn. connect ();

InputStream = urlConn. getInputStream ();

} Catch (MalformedURLException e ){

E. printStackTrace ();

} Catch (IOException e ){

E. printStackTrace ();

}

Return inputStream;

}

/* Access the xml format string returned by the remote (WebService) xml data and generate a local file */

Public static void helloOK (Document document, String savaFileURL ){

TransformerFactory transF = TransformerFactory. newInstance ();

Try {

Transformer transformer = transF. newTransformer ();

DOMSource source = new DOMSource (document );

Transformer. setOutputProperty (OutputKeys. ENCODING, "UTF-8 ");

Transformer. setOutputProperty (OutputKeys. INDENT, "YES ");

PrintWriter pw = new PrintWriter (new FileOutputStream (savaFileURL ));

StreamResult result = new StreamResult (pw );

Transformer. transform (source, result );

System. out. println ("xml file generated successfully! ");

} Catch (TransformerConfigurationException e ){

System. out. println (e. getMessage ());

} Catch (IllegalArgumentException e ){

System. out. println (e. getMessage ());

} Catch (FileNotFoundException e ){

System. out. println (e. getMessage ());

} Catch (TransformerException e ){

System. out. println (e. getMessage ());

}

}

}

I hope this article will help you with java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.