java类中
  
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "USAddress", propOrder = { "data" })  
 public class USAddress {
     String getData() {..};
      }
xml:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<USAddress xmlns="">
<ServiceName>Send</ServiceName>
<ContactID>20110218102422312</ContactID>
<Data xmlns=""><DataName>DestTermID</DataName><DataValue>12345</DataValue></Data>
</USAddress >
</soapenv:Body>
</soapenv:Envelope> 
xml文件放在哪里,让它跟java类关联~这个类USAddress是怎样加载xml的?大侠们帮帮忙!

解决方案 »

  1.   

    这个需要 JAX-WS 框架来处理 SOAP XML 与 JAXB 对象转换的。
      

  2.   

    思路很简单,通过XML文件读取类的名字,全路径包括包名,通过反射创建类的对象,在调用方法。需要动态调用的类public class HelloWorld {  public void sayHello(){    System.out.println("hello world");  }  public void sayHello(int i){    System.out.println("hello world");  }  public void sayHello(String[]a){    System.out.println("hello world");  }public static void main(String[] args) {  System.out.println("hello world");}}读取的xml文件<?xml version="1.0" encoding="UTF-8"?><element><import class="com.hd123.test.HelloWorld"/></element>主程序
    public class Test {  /**   * @param args   * @throws IOException   * @throws ParserConfigurationException    * @throws SAXException    * @throws ClassNotFoundException    * @input   */  public static void main(String[] args) throws Exception, ParserConfigurationException, SAXException, ClassNotFoundException {    DocumentBuilder builder;    builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();    if (args.length == 0)      return;    else {      InputStream in=null;      try {//读取XML文件        String filePath = args[0];        File file = new File(filePath);        in = new FileInputStream(file);        Document doc=builder.parse(in);        Element eRoot = doc.getDocumentElement();        NodeList nl = eRoot.getChildNodes();        for (int i = 0; i < nl.getLength(); i++) {          Node n = nl.item(i);          if (!(n instanceof Element))            continue;          Element e = (Element) n;          System.out.println(e.getNodeName());//取得类名          String className=e.getAttribute("class");          Class cl=Class.forName(className);//创建类实例对象          Object o=cl.newInstance();//动态调用方法          Method m=cl.getMethod("sayHello",  String[].class);          Object[] ob=new Object[1];          String arg[]=new String[1];          arg[0]="1";          ob[0]=arg;          m.invoke(o,ob);        }      } finally {        in.close();      }    }  }}
      

  3.   

    是的 但要xml文件放在什么地方,与java类在同一个目录吗,我不懂,刚接触
      

  4.   

    不需要这个 XML,这个应该是 SOAP Web 服务调用请求中的传入的 XML,如果用了 JAX-WS 框架的话,不会直接传个 XML 进去,而是传个 JAXB 的对象进去,JAX-WS 框架会自动序列化成 SOAP XML 的。你可以去看一下 JAX-WS 的实现 Apache CXF
      

  5.   

    网上查下apache cxf,rest服务,类似web service