toString字符串转换element文件格式

来源:互联网 发布:java工程师学费 编辑:程序博客网 时间:2024/06/05 14:23

package com.ultrapower.service;


import java.io.IOException;

import java.io.Reader;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;


public class POCUtil

{
 public static Element[] transStringToW3cElement(String xmlString)
 
 {
  Element[] elems = new Element[1];
  elems[0] = getElementByString(xmlString);
  return elems;
 }

 
 public static Element[] returnValidateInfo(int faultType,
         String errorMessage)
 {
  return transStringToW3cElement("<returnInfo><returnCode>-1</returnCode><description>"
          + errorMessage + "</description></returnInfo>");
  }

 
 private static Element getElementByString(String xmlString)
 
 {
  if (xmlString == null)
   xmlString = "<returnInfo><returnCode>-999</returnCode><description>undefined error</description></returnInfo>";
  Document doc = null;
  try
  {
   Reader strreader = new StringReader(xmlString);
   DocumentBuilder builder = DocumentBuilderFactory
           .newInstance().newDocumentBuilder();
   doc = builder.parse(new InputSource(strreader));
   } catch (ParserConfigurationException e)
  {
   e.printStackTrace();
   } catch (SAXException e)
  {
   e.printStackTrace();
   } catch (IOException e)
  {
   e.printStackTrace();
   } catch (Exception e)
  {
   e.printStackTrace();
   }
 
  return doc.getDocumentElement();
  }

 
 public static void main(String[] args)
 
 {
  }
 
}

 

原创粉丝点击