<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ufinterface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" account="0001" billtype="bscubas" isexchange="Y" proc="add" receiver="0001" replace="Y" sender="1004">
<basdoc>
<basdoc_head>
<pk_corp>0001</pk_corp>
<pk_areacl>110002</pk_areacl>
<custcode>11020001</custcode>
<custname>天津黄杰-中海</custname>
<custshortname>天津黄杰-中海</custshortname>
</basdoc_head>
</basdoc>
<basdoc>
<basdoc_head>
<pk_corp>0001</pk_corp>
<pk_areacl>110002</pk_areacl>
<custcode>11020002</custcode>
<custname>冀昆-3a003</custname>
<custshortname>冀昆-3A003</custshortname>
</basdoc_head>
</basdoc>
</ufinterface>
用了将近2个小时捣鼓XML,还是没整出来。。高手给个代码看下思路,感谢!问题。。取第一个的custname的值。。

解决方案 »

  1.   


    public String getLocationURL(HttpServletRequest req,String file,String item) {
    String filepath = req.getRealPath("WEB-INF/classes/com/feecc/util/sql")+"/"+file;
    String sql = "" ;

    try {
    Element el = this.getNodeByPath(filepath, item);
    //Node node = el.getFirstChild();
    //sql = node.getNodeValue() ;
                             这里是取第一个节点  你可以继续取 然后取第一个节点下面的 <custname>天津黄杰-中海</custname>
    if("".equals(sql) || sql == null) {
    throw new Exception("xml没有配置sqlpath:"+sql);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

    return sql ;
    }

    private Element getNodeByPath(String configfile,String item) throws Exception{
    Document doc = this.getXml(configfile);
    Element els = (Element)doc.getDocumentElement();

    NodeList nl = els.getChildNodes();
    for(int j=0;j<nl.getLength();j++){
    if(item.equals(nl.item(j).getNodeName())){
    els = (Element) nl.item(j);
    break;
    }
    }
    return els;

    }

    private Document getXml(String configFileName) throws Exception {
    try {
    DocumentBuilderFactory factory = DocumentBuilderFactory
    .newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(configFileName);
    doc.normalize();
    return doc;
    } catch (Exception e) {
    throw new Exception("get xml file error-->"+e.getMessage(),e.getCause());
    }
    }
      

  2.   


    恩,非常感谢提供的JAVA后台代码。我这是基于软件里面自带的javascript业务,写不了后台.lee兄有javascript这方面的思路吗?
      

  3.   

    你可以用反射来解析xml,如果想用反射写的话我这有。可以发到你的邮箱。
      

  4.   


    好,那麻烦你了。[email protected] thanks!
      

  5.   

    用Ajax获取XML返回responseXML,或者直接在页面上创建XML对象然后就是:
    var _value = xmlDoc.documentElement.getElementsByTagName('custname')[0].firstChild.nodeValue;
      

  6.   


    $(document).ready(function(){
        $("input#fetchData").click(function($e){
            $e.preventDefault();
            
            $.get(
                "xml1.xml",
                function(xml){
                    $("input#content").val($(xml).find("custname:first").text());
                }
            );
        });
    });IE8不支持,firefox下可以
      

  7.   


    我是在某软件上写javascript,所以Jquery包导入不了,不太适合.呵呵 thanks! 
      

  8.   


      var xmlDoc = null;
      try {
        var parser = new DOMParser();
        xmlDoc = parser.parseFromString(xmlString, "text/xml");
      }
      catch (e) {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(xmlString);
      }
      xmlDoc.getElementsByTagName("custname")[0].firstChild.data;用这方法加载然后 
      

  9.   

    你可以把JS框架中的XML扣出来不就行了
      

  10.   


    javascript懂点皮毛,也就做做验证什么的..呵呵还望高手指点下!
      

  11.   

    List elements = DTSParser.getElementList(document,
    "/schema:schema/schema:element");
    List listName = new ArrayList();
    Iterator iter = elements.iterator();
    while (iter.hasNext()) {
    Element foo = (Element) iter.next();
    ElementDe elementDe = new ElementDe();
    elementDe.setName(foo.attributeValue("name"));
    elementDe.setId(foo.attributeValue("id"));
    elementDe.setNillable(foo.attributeValue("nillable"));
    elementDe.setBalance(foo.attributeValue("balance"));
    elementDe.setType(foo.attributeValue("type"));
    elementDe.setSubstitutionGroup(foo
    .attributeValue("substitutionGroup"));
    elementDe.setPeriodType(foo.attributeValue("periodType"));
    if (elementDe.getSubstitutionGroup().trim().equals("xbrli:tuple")) { // element的SubstitutionGroup值为xbrli:tuple则type设为true
    // 方便判断SubstitutionGroup的值
    if (elementDe.getPeriodType() != null
    && !elementDe.getPeriodType().equals("")) {
    elementDe.setIsperType(true);
    }