import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class CommonSettingsXmlDAO
{
    private CommonSettingsXmlDAO()
    {
    }    public static Map loadCommonSettings(String location)
            throws IOException
    {
        if(location != null && !location.equals(""))
        {
            return CommonSettingsXmlDAO.getParamMappings(location);
        }
        else
        {
            
        }
    }
    private static Map getParamMappings(String url)
            throws IOException
    {
        HashMap paramMapping = new HashMap();        ConfigUtilities configUtilities = new ConfigUtilities();
        Document doc = configUtilities.parse(url , true);        if(doc != null)
        {
            configUtilities.setDocument(doc);
            ArrayList nodes = configUtilities.findNodes("con/mapping");            Node node = null;
            //遍历所有的<mappings>节点
            for(int i = 0 ; i < nodes.size() ; i++)
            {
                node = (Node)nodes.get(i);
                String paramName = getSubTagValue(node, "param-name");
                String paramValue = getSubTagValue(node, "param-value");
                //System.out.println("paramName = " + paramName);
                //System.out.println("paramValue = " + paramValue);
                if(paramName != null && paramValue != null)
                {
                    paramMapping.put(paramName.trim() , paramValue.trim());
                }
            }            return paramMapping;
        }
        else
        {
         }
    }    private static String getSubTagValue(Node node, String subTagName)
    {
        String returnString = "";        if (node != null && subTagName != null)
        {
            NodeList  children = node.getChildNodes();            for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++)
            {
                Node  child = children.item(innerLoop);                if ((child != null)
                    && (child.getNodeName() != null)
                    && child.getNodeName().equals(subTagName))
                {
                    //获取文本节点
                    Node grandChild = child.getFirstChild();
                    if (grandChild.getNodeValue() != null)
                    {
                        return grandChild.getNodeValue();
                    }
                }
            } // end inner loop
        }        return returnString;
    }    public static void main(String[] args)
    {
        try
        {
            CommonSettingsXmlDAO.loadCommonSettings("E:/yy.xml");
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }}

解决方案 »

  1.   

    谢谢了 不过有个问题 ConfigUtilities   这个类是什么地方来的?  还有这个代码好像是读XML文件     有将结果集写入XML文件的代码吗
      

  2.   

    写入和读出差不多,必须用第三方的工具,ConfigUtilities这个类是我们公司写的jar文件中的一个接口,这个我当然不能给你看了,希望你能从中知道思路就可以了。
      

  3.   

    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    这几个包从哪里来?
      

  4.   

    import java.sql.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
    import java.io.*;public class SelectStud { public static void main(String[] args) {
    String headerstring="<?xml version=\"1.0\"?>";
    String root="<students>";
    String rootend="</students>";

    try{
    File ff=new File("students.xml");
    ff.createNewFile();
    FileWriter fw=new FileWriter(ff);
    fw.write(headerstring);
    fw.write(root);

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:oradsn","wanchao","oracle");
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("select * from students");
    while(rs.next()){
    fw.write("<student>");
    fw.write("<stud_id>");
    String stud_id=rs.getString("stud_id");
    fw.write(stud_id);
    fw.write("</stud_id>");
    fw.write("<stud_name>");
    String stud_name=rs.getString("stud_name");
    fw.write("stud_name");
    fw.write("</stud_name>");
    fw.write("</student>");
    }
    fw.write(rootend);
    fw.close();
    }
    catch(Exception e){
    System.out.println(e);
    }
    }
    }
      

  5.   

    晕倒,这种东西还要麻烦别人找源代码吗?简单的很啊,w3c规范,jdom,dom4j,castor,jibx....太多的技术供你选了,实在不行自己手工在程序中输出都可以的啊。楼主还真是懒人
      

  6.   

    那有这么麻烦..都什么年代了..基本上大型的数据库管理系统如,sqlserver ,db2,oracle应该都提供了xml格式的输出.就直接一个select语句就搞定的....
      

  7.   

    package mycrm.extra.xml;import java.io.StringReader;import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.XMLOutputter;
    public XMLParser() {
    super();
    }
    public static String output(Document doc) throws Exception{
    XMLOutputter output=new XMLOutputter(); return output.outputString(doc);


    }
    public static String output(Element element) throws Exception{
    XMLOutputter output=new XMLOutputter(); return output.outputString(element);


    }
    public static Document parse(String strXML) throws Exception{
    SAXBuilder sax=new SAXBuilder();

    return sax.build(new StringReader(strXML));


    }
    }
      

  8.   

    上面就是使用JDOM方式读取XML文件的!
      

  9.   

    在sqlserver中,用sql解决:
    DECLARE @idoc int
    DECLARE @doc varchar(1000)
    --sample XML document
    SET @doc ='
    <root>
      <Customer cid= "C1" name="Janine" city="Issaquah">
          <Order oid="O1" date="1/20/1996" amount="3.5" />
          <Order oid="O2" date="4/30/1997" amount="13.4">Customer was very satisfied
          </Order>
       </Customer>
       <Customer cid="C2" name="Ursula" city="Oelde" >
          <Order oid="O3" date="7/14/1999" amount="100" note="Wrap it blue 
                 white red">
                <Urgency>Important</Urgency>
                Happy Customer.
          </Order>
          <Order oid="O4" date="1/20/1996" amount="10000"/>
       </Customer>
    </root>
    '
    -- Create an internal representation of the XML document.
    EXEC sp_xml_preparedocument @idoc OUTPUT, @doc-- Execute a SELECT statement using OPENXML rowset provider.
    SELECT *
    FROM OPENXML (@idoc, '/root/Customer/Order', 1)
          WITH (oid     char(5), 
                amount  float, 
                comment ntext 'text()')
    EXEC sp_xml_removedocument @idoc