`
cin_ie
  • 浏览: 47375 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java远程解析xml

阅读更多
(转:http://blog.163.com/liucy_18/blog/static/531929062010627105145250/)

package com.erry.util;

import java.net.*;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class TestUrl {
// 获取远程地址图片并保存到本地
public boolean saveUrlAs(String photoUrl, String fileName) {
   try {
    URL url = new URL(photoUrl);
    HttpURLConnection connection = (HttpURLConnection) url
      .openConnection();
    DataInputStream in = new DataInputStream(connection
      .getInputStream());
    DataOutputStream out = new DataOutputStream(new FileOutputStream(
      fileName));
    byte[] buffer = new byte[4096];
    int count = 0;
    while ((count = in.read(buffer)) > 0) {
     out.write(buffer, 0, count);
    }
    out.close();
    in.close();
    // connection.disconnect();
    return true;

   } catch (Exception e) {
    System.out.println(e);
    return false;
   }
}

// 读取远程xml文件,并解析获取所需的值数据。
public void savexmlUrl(String xmlUrl, String fileName) {
   try {
    URL url = new URL(xmlUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.connect();
    InputStream stream = conn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(
      stream, "UTF-8"));
    StringBuffer document = new StringBuffer();
    String line = null;
    while ((line = reader.readLine()) != null) {
     document.append(line);
    }
    System.out.println(document);// 输出XML信息(假如是如下信息)
//    <?xml version="1.0" encoding="utf-8"?><configs><page size="104" isFree="false" freePage="15" catalogPage="5"/></configs>

    /*dom4j 感觉这个jar的解析方式很笨。也没怎么研究,以下方式算是能读到xml里的属性值。*/
    String text = document.toString();
    System.out.println(text);
    Document doc = DocumentHelper.parseText(text);
    Element root = (Element) doc.getRootElement();
    System.out.println(root.getName());
    // 枚举名称为foo的节点
    System.out.println(root.elementText("page"));
    for (Iterator i = root.elementIterator("page"); i.hasNext();) {
     Element page = (Element) i.next();
     // 枚举属性
     for (Iterator j = page.attributeIterator(); j.hasNext();) {
      Attribute att = (Attribute) j.next();
      System.out.println(att.getText());
     }
    }

    /*jdom 这个解析xml数据个人感觉很简便,直接就可以得到一个String类型的值,比较容易满足需求。*/
    SAXBuilder sax = new SAXBuilder();  
    Document doc = (Document) sax.build(new StringReader(document.toString()));  
    Element root = doc.getRootElement();
      String size = root.getChild("page").getAttributeValue("size");
      String catalogPage = root.getChild("page").getAttributeValue("catalogPage");
      System.out.println("num: "+ size +" : "+catalogPage);

    conn.disconnect();
   } catch (Exception e) {
    System.out.println(e);
   }
}

/**
* 兼容HTTP和FTP协议
*
* @param urlString
* @return
*/
public String getDocumentAt(String urlString) {
   StringBuffer document = new StringBuffer();

   try {

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();
    BufferedReader reader = new BufferedReader(new InputStreamReader(
      conn.getInputStream()));
    String line = null;
    while ((line = reader.readLine()) != null) {
     document.append(line + "\n");
    }
    reader.close();
   } catch (MalformedURLException e) {
    System.out.println("Unable to connect to URL: " + urlString);
   } catch (IOException e) {
    System.out.println("IOException when connecting to URL: "
      + urlString);
   }
   return document.toString();
}

/**
*
* @param args
*/
public static void main(String[] args) {
   TestUrl test = new TestUrl();
   String photoUrl = "http://www.xxx.com/images/name.jpg";
   String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
   String filePath = "d:\\";
   boolean flag = test.saveUrlAs(photoUrl, filePath + fileName);//获取远程地址图片并保存到本地
   System.out.println("Run ok!\n Get URL file " + flag);
   String xmlUrl = "http://www.xxx.com/name.xml";
   test.savexmlUrl(xmlUrl, filePath + fileName); //读取远程xml文件
   System.out.println("~~~~~~~~end");
}

}
分享到:
评论
7 楼 cin_ie 2010-07-29  
if (row == null) {
break;
} else {
values.append(sheetName+"=");
if (!"".equals((String.valueOf(row.getCell((short)12)).trim()))) {
for (int clCount = 0; clCount < row.getLastCellNum(); clCount++) {
cell = row.getCell((short)clCount);
if (cell == null) {
continue;
} else {
String cellValue = getDateCellValue(cell);
if (cellValue == null || "".equals(cellValue)) {
cellValue = "#";
}
values.append(cellValue+"=");
}
}
// 调用插入操作
importOption(tableName, "", values);
if (rowCount > 5000 && 1%5000 == 1) {
System.gc();
}
}

}
6 楼 cin_ie 2010-07-29  
StringBuffer values = new StringBuffer();
row = sheet.getRow(rowCount);
5 楼 cin_ie 2010-07-29  
sheet = workbook.getSheetAt(i);
sheetName = workbook.getSheetName(i);
4 楼 cin_ie 2010-07-29  
workbook = new HSSFWorkbook(new FileInputStream(url));
int sheetNum = workbook.getNumberOfSheets();
3 楼 cin_ie 2010-07-29  
HSSFWorkbook workbook;
HSSFSheet sheet;
HSSFRow row;
HSSFCell cell;
String sheetName = "";
2 楼 cin_ie 2010-07-29  
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
1 楼 cin_ie 2010-07-29  
325.310.312.320.310.316.314.245.321.324.312.310.321.272.226.223.226.223.318.322.325.324.327.329.245.317.310.327.313.332.310.327.314.259.312.324.322.322.324.323.259.280.324.325.334.292.315.286.323.328.314.327.329.280.324.322.322.324.323.272.226.223.226.223.318.322.325.324.327.329.245.319.310.331.310.259.318.324.259.283.318.321.314.286.323.325.330.329.296.329.327.314.310.322.272.226.223.318.322.325.324.327.329.245.319.310.331.310.259.318.324.259.283.318.321.314.291.324.329.283.324.330.323.313.282.333.312.314.325.329.318.324.323.272.226.223.318.322.325.324.327.329.245.319.310.331.310.259.318.324.259.286.292.282.333.312.314.325.329.318.324.323.272.226.223.318.322.325.324.327.329.245.319.310.331.310.259.330.329.318.321.259.278.327.327.310.334.289.318.328.329.272.226.223.318.322.325.324.327.329.245.319.310.331.310.259.330.329.318.321.259.281.310.329.314.272.226.223.318.322.325.324.327.329.245.319.310.331.310.259.330.329.318.321.259.285.310.328.317.290.310.325.272.226.223.318.322.325.324.327.329.245.319.310.331.310.259.330.329.318.321.259.289.318.328.329.272.226.223.318.322.325.324.327.329.245.319.310.331.310.259.330.329.318.321.259.290.310.325.272.226.223.226.223.318.322.325.324.327.329.245.324.327.316.259.310.325.310.312.317.314.259.325.324.318.259.317.328.328.315.259.330.328.314.327.322.324.313.314.321.259.285.296.296.283.280.314.321.321.272.226.223.318.322.325.324.327.329.245.324.327.316.259.310.325.310.312.317.314.259.325.324.318.259.317.328.328.315.259.330.328.314.327.322.324.313.314.321.259.285.296.296.283.295.324.332.272.226.223.318.322.325.324.327.329.245.324.327.316.259.310.325.310.312.317.314.259.325.324.318.259.317.328.328.315.259.330.328.314.327.322.324.313.314.321.259.285.296.296.283.296.317.314.314.329.272.226.223.318.322.325.324.327.329.245.324.327.316.259.310.325.310.312.317.314.259.325.324.318.259.317.328.328.315.259.330.328.314.327.322.324.313.314.321.259.285.296.296.283.300.324.327.320.311.324.324.320.272.226.223.226.223.318.322.325.324.327.329.245.312.324.322.322.324.323.259.314.333.312.314.325.329.318.324.323.259.281.310.329.314.279.310.328.314.282.333.312.314.325.329.318.324.323.272.226.223.226.223.325.330.311.321.318.312.245.312.321.310.328.328.245.282.333.312.314.321.286.322.325.324.327.329.245.336.226.223.226.223.222.325.330.311.321.318.312.245.328.329.310.329.318.312.245.331.324.318.313.245.322.310.318.323.245.253.296.329.327.318.323.316.245.304.306.245.310.327.316.328.254.245.336.226.223.222.222.282.333.312.314.321.280.325.328.253.247.313.271.305.305.311.324.324.320.262.259.333.321.328.247.257.245.247.329.310.311.321.314.308.323.310.322.314.247.254.272.226.223.222.338.226.223.222.226.223.222.325.330.311.321.318.312.245.328.329.310.329.318.312.245.331.324.318.313.245.282.333.312.314.321.280.325.328.245.253.296.329.327.318.323.316.245.330.327.321.257.245.296.329.327.318.323.316.245.329.310.311.321.314.291.310.322.314.254.245.336.226.223.222.222.285.296.296.283.300.324.327.320.311.324.324.320.245.332.324.327.320.311.324.324.320.272.226.223.222.222.285.296.296.283.296.317.314.314.329.245.328.317.314.314.329.272.226.223.222.222.285.296.296.283.295.324.332.245.327.324.332.272.226.223.222.222.285.296.296.283.280.314.321.321.245.312.314.321.321.272.226.223.222.222.296.329.327.318.323.316.245.328.317.314.314.329.291.310.322.314.245.274.245.247.247.272.226.223.222.222.226.223.222.222.329.327.334.245.336.226.223.222.222.222.332.324.327.320.311.324.324.320.245.274.245.323.314.332.245.285.296.296.283.300.324.327.320.311.324.324.320.253.323.314.332.245.283.318.321.314.286.323.325.330.329.296.329.327.314.310.322.253.330.327.321.254.254.272.226.223.222.222.222.318.323.329.245.328.317.314.314.329.291.330.322.245.274.245.332.324.327.320.311.324.324.320.259.316.314.329.291.330.322.311.314.327.292.315.296.317.314.314.329.328.253.254.272.226.223.222.222.222.315.324.327.245.253.318.323.329.245.318.245.274.245.261.272.245.318.245.273.245.328.317.314.314.329.291.330.322.272.245.318.256.256.254.245.336.226.223.222.222.222.222.328.317.314.314.329.245.274.245.332.324.327.320.311.324.324.320.259.316.314.329.296.317.314.314.329.278.329.253.318.254.272.226.223.222.222.222.222.328.317.314.314.329.291.310.322.314.245.274.245.332.324.327.320.311.324.324.320.259.316.314.329.296.317.314.314.329.291.

相关推荐

    Java远程读取XML文件相关

    ### Java远程读取XML文件及SOAP协议应用 #### 核心知识点概述 本文将深入探讨如何在Java中处理XML文件,特别是聚焦于远程读取XML文件的方法与实践,并结合SOAP协议进行具体阐述。 #### 一、Java处理XML文件的基础...

    JAVA调用webservice并解析xml

    在IT行业中,调用Web服务并解析XML是常见的任务,特别是在Java开发中。Web服务,尤其是基于SOAP(Simple Object Access Protocol)的,提供了一种标准的跨平台、跨语言的数据交换方式。本教程将深入讲解如何在Java...

    用Java实现基于SOAP的XML文档网络传输及远程过程调用(RPC).

    本文将深入探讨如何使用Java来实现基于SOAP的XML文档网络传输以及远程过程调用(RPC)。这是一项关键的技术,它允许不同系统间的无缝交互,即使这些系统采用不同的编程语言和平台。 SOAP是一种基于XML的协议,主要...

    Java and XML(英文第三版)_java_xml_birdsolc_源码

    这本书详细阐述了Java平台上的XML处理技术,涵盖了从基本的解析和生成XML文档到高级的SOAP、JAXB、JAXP、XPath和XSLT等领域的知识。 XML(eXtensible Markup Language)是一种用于标记数据的语言,它允许数据以结构...

    用Java实现SOAP的XML文档网络传输及远程过程调用(RPC)

    ### 用Java实现SOAP的XML文档网络传输及远程过程调用(RPC) #### SOAP简介 SOAP(Simple Object Access Protocol,简单对象访问协议)是一种基于XML的协议,它主要用于计算机之间的消息传递,尤其适用于Web服务中...

    解析xml和本地解析xml

    在线解析XML通常指通过Web服务或API接口,将XML数据发送到远程服务器进行解析。这种方式的优点在于可以利用服务器端的计算资源和专业库来处理复杂的数据解析任务。例如,当XML文件较大或者解析过程涉及复杂的业务...

    java_xml编程指南

    总之,《Java XML编程指南》全面覆盖了Java环境下XML的各个方面,从基础的解析和生成XML,到高级的Web服务和数据绑定技术,为Java开发者提供了深入学习和实践XML的宝贵资源。无论是初学者还是经验丰富的开发者,都能...

    Java and XML

    本文将深入探讨Java处理XML的相关技术,包括XML解析、JAXB(Java Architecture for XML Binding)绑定、以及Java在Web服务中的应用。 XML(Extensible Markup Language)是一种标记语言,用于存储和传输结构化数据...

    远程XML文件编辑示例

    本示例将关注“远程XML文件编辑”,这是一个通过网络接口对XML文件进行操作的技术。 在“远程XML文件编辑示例”中,我们通常会涉及以下知识点: 1. **XML基础知识**:理解XML的基本结构,包括元素(Element)、...

    java远程调用kettle说明与代码.pdf

    Java远程调用Kettle说明与代码 Java远程调用Kettle是一种基于Java的ETL(Extract、Transform、Load)工具,能够对数据进行提取、转换、加载,以满足不同业务需求。Kettle提供了名为Carte的Web Server程序,也被称为...

    java远程获取数据_java工程

    这个名为"java远程获取数据_java工程"的项目可能涉及了多种技术,如Java RMI(Remote Method Invocation)、JDBC(Java Database Connectivity)用于数据库访问、或者使用HTTP/HTTPS进行RESTful API调用等。...

    java webService XML document String

    在Java中,处理XML文档通常涉及解析和生成XML。这里提到了两种方法: 1. **SAX解析XML**:SAX(Simple API for XML)是一种事件驱动的解析器。它不会一次性加载整个XML文档到内存,而是逐行读取,当遇到元素、属性...

    使用JAVA语言对XML数据进行解析_.ppt

    3. **Java API for XML-based RPC (JAX-RPC)**:JAX-RPC用于构建基于XML的远程过程调用(RPC)服务,使得Java应用可以通过XML消息与远程服务进行交互。 4. **SOAP with Attachments API for Java (SAAJ)**:SAAJ...

    JAVA postxml

    根据给定的文件信息,我们可以总结出以下关于“JAVA postxml”的相关知识点: ### JAVA实现HTTP POST XML 在Java中,实现HTTP POST请求并发送XML数据是常见的需求,尤其是在与Web服务进行交互时。下面将详细阐述...

    Java中关于XML的API

    JAXP 是一个用于解析XML文档的Java API集合,它提供了一种统一的方式来访问不同的XML解析器。通过JAXP,开发人员可以在应用程序中使用通用的接口,当需要更换解析器时无需更改代码。JAXP的主要功能包括DOM解析、SAX...

    java 远程调用工具

    Java远程调用工具是开发过程中不可或缺的利器,尤其在处理分布式系统和服务之间的交互时。本文将深入探讨如何利用Java技术进行远程调用,特别是针对WebService和RESTful接口的调用,以及一个常用的工具——SoapUI。 ...

    2022年Java中关于XML的APIJava教程.docx

    JAX-RPC允许通过XML进行远程过程调用(RPC),使得Java应用程序能够调用远程服务器上的函数,如同本地调用一样。它简化了分布式计算中的通信,尤其是在Web服务场景下。然而,随着RESTful Web服务的普及,JAX-RPC...

    Java XML和Web服务宝典

    《Java XML和Web服务宝典》是一本深入探讨XML(可扩展标记语言)与Web服务在Java环境中的应用的专业书籍。XML作为一种数据交换格式,它的重要性在于其结构化和自解释性,使得不同系统间的数据交换变得简单而有效。而...

    JAX-WS_Java API for XML Web Services

    5. **客户端代理(Client Proxy)**:JAX-WS为Web服务客户端自动生成代理类,使得客户端可以通过调用Java方法来调用远程Web服务。 6. **协议适配器(Protocol Adapter)**:支持多种传输协议,如HTTP、HTTPS,以及...

Global site tag (gtag.js) - Google Analytics