夏天/isummer

Sun of my life !Talk is cheap, Show me the code! 追风赶月莫停留,平芜尽处是春山~

博客园 首页 新随笔 联系 管理

问题描述[2015-08-05]:


 

  在做一个Web Service 的数据交互安全的框架,用到对SOAP报文的修改,出现了一个怪异的问题,在本地Windows+tomcate6测试通过,可以正常解析,但是上服务器后就出现异常,提示相关节点值不能正常解析异常。举例一个dom节点结构如下图所示:

    <visitorInfo>
      <visitorMark>
          systemadmindb
      </visitorMark>
    </visitorInfo>

   Element visitorInforsElement = (Element) hdr.getElementsByTagName("visitorInfo").item(0);    
     visitorMark = visitorInforsElement.getElementsByTagName("visitorMark").item(0).getFirstChild().getTextContent();

  //提示输出信息提示visitorMark的值为null,但是在本地测试是可以获取标签visitorMark中的值的???

问题分析


  既然问题不能再linux环境下正常解析节点的值,那把dom的Element元素的API都测试一下,看看其在linux环境下那些事可以获取结点中的内容的,相关Element元素的API调用测试如下所示,每个API紧邻下一行的注释代表输出结果。

 1   NodeList visitorMarkNodeList = visitorInforsElement.getElementsByTagName("visitorMark");
 2     System.out.println("//:" + visitorMarkNodeList.toString());
 3     //:weblogic.xml.domimpl.DeepNodeListImpl@2c27d380
 4     //visitorMarkNodeList.item(0),得到的是一个结点的类型(有结点名字,有结点的文本信息,结点的值没有意义)
 5     System.out.println("//:" + visitorMarkNodeList.item(0).getNodeName());
 6     //:visitorMark
 7     System.out.println("//:" + visitorMarkNodeList.item(0).getNodeValue());
 8     //:null
 9     System.out.println("//:" + visitorMarkNodeList.item(0).getTextContent());
10     //:systemadmindb
11     System.out.println("//:" + visitorMarkNodeList.item(0).getFirstChild() == null ? "NULL值" : "可以获得firstChild");
12     //visitorMarkNodeList.item(0).getFirstChild() ,得到的不是一个规范的结点(没有结点名字,只有结点值,结点的文本信息没有意义,就是一个结点值的内容)
13     //可以获得firstChild
14     System.out.println("//:" + visitorMarkNodeList.item(0).getFirstChild().getNodeName());
15     //:#text
16     System.out.println("//:" + visitorMarkNodeList.item(0).getFirstChild().getNodeValue());
17     //:systemadmindb
18     System.out.println("//:" + visitorMarkNodeList.item(0).getFirstChild().getTextContent());
19     //:
20     
21     显示结果:
22     //:weblogic.xml.domimpl.DeepNodeListImpl@2c27d380
23     //:visitorMark
24     //:null
25     //:systemadmindb
26     可以获得firstChild
27     //:#text
28     //:systemadmindb
29     //:

 

 

  调试结果如下,通过测试过程明白了问题出现位置了,不同的xml片段结果不一样,需要调用不同的api进行获取:

    相关函数的说明:
    ********************
    String org.w3c.dom.Node.getTextContent() throws DOMException
    //返回,指定Node的内容:This attribute returns the text content of this node and its descendants.
    ********************
    Open Declaration Node org.w3c.dom.Node.getFirstChild()
    //返回,第一个node节点:The first child of this node. If there is no such node, this returns null.
    ********************
    Open Declaration String org.w3c.dom.Node.getNodeValue() throws DOMException
    //返回,Node的内容:The value of this node,
    ********************
    String org.w3c.dom.Node.getNodeName()
    //返回,Node的名字:The name of this node,

posted on 2015-07-15 21:58  夏天/isummer  阅读(332)  评论(0)    收藏  举报