(Original) java xml data access WebService returns an instance of students into the local file

Reprinted please indicate the source: http://eric-619.javaeye.com/blog/692838

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: Pure java interface to access remote WebService return xml formatted data stored in the local
* /
public class DomXMLString (
private static String SERVICES_HOST = "www.webxml.com.cn";
/ / Remote url WebService Interface
private static String NETDATA_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince";
/ / Interface to access the remote WebService return xml formatted data stored in the local absolute path
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);
)

/ * Returns 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); / / specific webService related
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 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); / / specific webService related
urlConn.connect ();
inputStream = urlConn.getInputStream ();
) Catch (MalformedURLException e) (
e.printStackTrace ();
) Catch (IOException e) (
e.printStackTrace ();
)
return inputStream;
)

/ * Access the remote (WebService) xml data back to the xml format string and Health as the 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 ("generate xml file 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 ());
)
)

)

分类:Java 时间:2010-06-17 人气:168
分享到:
blog comments powered by Disqus

相关文章

  • Solution org.w3c.dom.DOMException: No such Localname for SOAP URI exception 2010-06-23

    WebService with CXF is to do in the face org.w3c.dom.DOMException: No such Localname for SOAP URI is usually joined axis.jar exception to this package. Solution: the main reason is: xml-api.jar the jar in the meddling. Usually we use in building proj

  • JAVA操作XML的完整例子--W3C DOM篇 2013-02-21

    这是一个用JAVA W3C DOM 进行XML操作的例子,包含了查询.增加.修改.删除.保存的基本操作.较完整的描述了一个XML的整个操作流程.适合刚入门JAVA XML操作的朋友参考和学习. 假设有XML文件:test1.xml <? xml version="1.0" encoding="UTF-8" ?> < books > < book > < name > 哈里波特 </ name > <

  • java xml parsing and import database (dom4j) 2010-07-11

    java xml parsing and import database (dom4j) import java.io.File; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.

  • xml jdk原生api生成和解析---dom解析 2012-12-30

    解析器读入整个文档,然后构建一个驻留内存的树结构. 优点:整个文档树在内存中,便于操作:支持删除.修改.重新排列等多种功能: 缺点:将整个文档调入内存(包括无用的节点),浪费时间和空间: 使用场合:一旦解析了文档还需多次访问这些数据:硬件资源充足(内存.CPU). import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.Pri

  • java operation xml w3c and xml store your picture files 2010-03-29

    // File 1 package com.kelsen.beans.imagehelper; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputS

  • java.lang.NoClassDefFoundError: javax.swing.tree.TreeNode is a restricted class 2010-09-13

    Today in the GAE configure Struts in a lot of problems, the following is one of the Problem accessing /hello.action. Reason: java.lang.NoClassDefFoundError: javax.swing.tree.TreeNode is a restricted class. Please see the Google App Engine developer's

  • java.lang.NoClassDefFoundError: javax / el / ExpressionFactory 2010-10-25

    Today myeclipse6.5 written before the development environment will be deployed to a web project tomcat, deployment after the start, error, exception information is as follows: Information: XML validation disabled 2010-10-25 9:58:42 org.apache.catalin

  • Error: java.lang.NoClassDefFoundError: javax / persistence / Cacheable 2010-12-10

    Error: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax / p

  • java.lang.NoSuchMethodError: javax.servlet.ServletContext.getContextPath () 2009-05-02

    java.lang.NoSuchMethodError: javax.servlet.ServletContext.getContextPath () Ljava / lang / String; Today, the deployment went so far been reported so a mistake. Investigation found that, getContextPath () only under the servlet 2.5, and servlet 2.4 i

  • java.lang.NoSuchMethodError: javax.servlet.ServletContext.getContextPath () Ljava / l 2010-04-13

    java.lang.NoSuchMethodError: javax.servlet.ServletContext.getContextPath () Ljava / lang / String; 2009 Year 01 Monday, 12 4:45 PM always encountered this problem recently, looking for a search found tomcar jsf-under asi.jfar, jsf-implk.jar two packa

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.055 (s). 11 q(s)