以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  >>>跪求<<<,一个DOM的问题 (在线等)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=12787)


--  作者:asapple
--  发布时间:12/15/2004 3:57:00 PM

--  >>>跪求<<<,一个DOM的问题 (在线等)
<?xml version="1.0" encoding = "GB2312"?>
  <ICBCYH>
    <opRep>
       <opName>NCQueryBalanceListOp</opName>
 <opResult>
    <retCode>错误返回码  错误描述在errMsg 中</retCode>
    <errMsg>错误描述</errMsg>
    <PackageID>包序列ID</PackageID>
    <userID>企业代码</userID>
    <RepReserved1>返回包备用字段1</RepReserved1>
    <RepReserved2>返回包备用字段2</RepReserved2>
 </opResult>
 <opResultSet>
    <opResult>
       <Account_num>帐号1</Account_num>
       <Account_cur>币种1</Account_cur>
       <Acc_balance>昨日余额1</Acc_balance>
       <Balance>余额1</Balance>
       <Usable_balance>可用余额1</Usable_balance>
       <Acct_property>帐户属性1</Acct_property>
       <RepReserved3>返回包备用字段31</RepReserved3>
       <RepReserved4>返回包备用字段41</RepReserved4>
             </opResult>
             <opResult>
       <Account_num>帐号2</Account_num>
       <Account_cur>币种2</Account_cur>
       <Acc_balance>昨日余额2</Acc_balance>
       <Balance>余额2</Balance>
       <Usable_balance>可用余额2</Usable_balance>
       <Acct_property>帐户属性2</Acct_property>
       <RepReserved3>返回包备用字段32</RepReserved3>
       <RepReserved4>返回包备用字段42</RepReserved4>
             </opResult>
             <opResult>
                 .
                 .
                 .
             </opResult>
 </opResultSet>
   </opRep>
</ICBCYH>

下面的是一个XML,我用的DOM来做的,我需要取除第一个<opResult>节点之外的所有内容,之前我都是用NodeList newNodeList = doc.getElementsByTagName("opResult")这个方法来取的(因为之前的结构比较简单),现在遇到了这样的问题,我不知道用什么方法可以搞定,求各位帮忙!!!!


--  作者:wxb_nudt
--  发布时间:12/15/2004 6:38:00 PM

--  
没时间看了,我先给个意见,先用xslt转换一下,把结果存储在临时文件中tmp.xml,然后再用dom来取是不是方便很多呢?
--  作者:wxb_nudt
--  发布时间:12/15/2004 6:54:00 PM

--  
我用xslt实现了你想要的东东:
你的xml源文件opResult.xml如下:
<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="opResult.xsl"?>
<ICBCYH>
 <opRep>
  <opName>NCQueryBalanceListOp</opName>
  <opResult>
   <retCode>错误返回码  错误描述在errMsg 中</retCode>
   <errMsg>错误描述</errMsg>
   <PackageID>包序列ID</PackageID>
   <userID>企业代码</userID>
   <RepReserved1>返回包备用字段1</RepReserved1>
   <RepReserved2>返回包备用字段2</RepReserved2>
  </opResult>
  <opResultSet>
   <opResult>
    <Account_num>帐号1</Account_num>
    <Account_cur>币种1</Account_cur>
    <Acc_balance>昨日余额1</Acc_balance>
    <Balance>余额1</Balance>
    <Usable_balance>可用余额1</Usable_balance>
    <Acct_property>帐户属性1</Acct_property>
    <RepReserved3>返回包备用字段31</RepReserved3>
    <RepReserved4>返回包备用字段41</RepReserved4>
   </opResult>
   <opResult>
    <Account_num>帐号2</Account_num>
    <Account_cur>币种2</Account_cur>
    <Acc_balance>昨日余额2</Acc_balance>
    <Balance>余额2</Balance>
    <Usable_balance>可用余额2</Usable_balance>
    <Acct_property>帐户属性2</Acct_property>
    <RepReserved3>返回包备用字段32</RepReserved3>
    <RepReserved4>返回包备用字段42</RepReserved4>
   </opResult>
  </opResultSet>
 </opRep>
</ICBCYH>

我写的xsl文件opResult.xsl如下(它将所有你想要的opResult元素放到opResultSet中,然后放到一个根元素resultRoot中)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output indent="yes"/>
 <xsl:template match="/">
  <resultRoot>
   <xsl:apply-templates select="ICBCYH/opRep/opResultSet"/>
  </resultRoot>
 </xsl:template>
 <xsl:template match="opResultSet">
  <xsl:for-each select="opResult">
  <opResultSet>
   <Account_num>
    <xsl:value-of select="Account_num"/>
   </Account_num>
   <Account_cur>
    <xsl:value-of select="Account_cur"/>
   </Account_cur>
   <Acc_balance>
    <xsl:value-of select="Acc_balance"/>
   </Acc_balance>
   <Balance>
    <xsl:value-of select="Balance"/>
   </Balance>
   <Usable_balance>
    <xsl:value-of select="Usable_balance"/>
   </Usable_balance>
   <Acct_property>
    <xsl:value-of select="Acct_property"/>
   </Acct_property>
   <RepReserved3>
    <xsl:value-of select="RepReserved3"/>
   </RepReserved3>
   <RepReserved4>
    <xsl:value-of select="RepReserved4"/>
   </RepReserved4>
  </opResultSet>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

执行后得到这样的文件,你可以修改一下符合你的要求:
<?xml version="1.0" encoding="UTF-16"?>
<resultRoot xmlns:fo="http://www.w3.org/1999/XSL/Format">
<opResultSet>
<Account_num>帐号1</Account_num>
<Account_cur>币种1</Account_cur>
<Acc_balance>昨日余额1</Acc_balance>
<Balance>余额1</Balance>
<Usable_balance>可用余额1</Usable_balance>
<Acct_property>帐户属性1</Acct_property>
<RepReserved3>返回包备用字段31</RepReserved3>
<RepReserved4>返回包备用字段41</RepReserved4>
</opResultSet>
<opResultSet>
<Account_num>帐号2</Account_num>
<Account_cur>币种2</Account_cur>
<Acc_balance>昨日余额2</Acc_balance>
<Balance>余额2</Balance>
<Usable_balance>可用余额2</Usable_balance>
<Acct_property>帐户属性2</Acct_property>
<RepReserved3>返回包备用字段32</RepReserved3>
<RepReserved4>返回包备用字段42</RepReserved4>
</opResultSet>
</resultRoot>


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms