写了一个操作XML格式String的类

String内容:

<FlexCell><DocumentProperties><LastSaved>2009-02-13 09:02:23</LastSaved><Version>4.2.0</Version></DocumentProperties><GridProperties><BackColorBkg>#808080</BackColorBkg><BackColorFixedSel>#c0c9e0</BackColorFixedSel><DisplayRowIndex>True</DisplayRowIndex><ReadOnlyFocusRect>Dot</ReadOnlyFocusRect><ScreenTwipsPerPixelX>15</ScreenTwipsPerPixelX><ScreenTwipsPerPixelY>15</ScreenTwipsPerPixelY><DefaultRowHeight>18</DefaultRowHeight><Rows>14</Rows><Cols>6</Cols><FixedRows>1</FixedRows><FixedCols>1</FixedCols><ActiveCell Row1="5" Col1="3" Row2="5" Col2="4"/><Selection Row1="5" Col1="3" Row2="5" Col2="4"/></GridProperties><PageSetup><HeaderAlignment>Left</HeaderAlignment><FooterAlignment>Center</FooterAlignment><HeaderFont FontName="MS Sans Serif" Size="8.25"/><FooterFont FontName="MS Sans Serif" Size="8.25"/><PaperSize>9</PaperSize><Orientation>Portrait</Orientation><PaperWidth>21</PaperWidth><PaperHeight>29.7</PaperHeight><LeftMargin>2.5</LeftMargin><RightMargin>2.5</RightMargin><TopMargin>3</TopMargin><BottomMargin>3</BottomMargin><HeaderMargin>2</HeaderMargin><FooterMargin>2</FooterMargin></PageSetup><Columns><Column Col="0"><Type>TextBox</Type><Width>40</Width><Alignment>GeneralGeneral</Alignment></Column><Column Col="1"><Type>TextBox</Type><Width>131</Width><Alignment>GeneralGeneral</Alignment></Column><Column Col="2"><Type>TextBox</Type><Width>257</Width><Alignment>GeneralGeneral</Alignment></Column><Column Col="3"><Type>TextBox</Type><Width>99</Width><Alignment>GeneralGeneral</Alignment></Column><Column Col="4"><Type>TextBox</Type><Width>175</Width><Alignment>GeneralGeneral</Alignment></Column><Column Col="5"><Type>TextBox</Type><Width>156</Width><Alignment>GeneralGeneral</Alignment></Column><Column Col="6"><Type>TextBox</Type><Width>141</Width><Alignment>GeneralGeneral</Alignment></Column></Columns><Rows><Row Height="51">1</Row><Row Height="46">2</Row><Row Height="46">3</Row><Row Height="46">4</Row><Row Height="46">5</Row><Row Height="46">6</Row><Row Height="46">7</Row><Row Height="46">8</Row><Row Height="46">9</Row><Row Height="46">10</Row><Row Height="46">11</Row><Row Height="46">12</Row><Row Height="46">13</Row><Row Height="53">14</Row></Rows><Fonts><Font ID="f0" FontName="宋体" Size="9"/><Font ID="f1" FontName="楷体_GB2312" Size="18"/><Font ID="f2" FontName="楷体_GB2312" Size="14.25"/></Fonts><Styles><Style ID="s0"><Border/><Alignment>GeneralGeneral</Alignment></Style><Style ID="s1"><Border Left="Thin" Right="Thin" Top="Thin" Bottom="Thin"/><Alignment>CenterCenter</Alignment></Style><Style ID="s2"><Border Left="Thin" Right="Thin" Top="Thin" Bottom="Thin"/><Alignment>CenterCenter</Alignment><Locked>True</Locked><Fill>True</Fill><BackColor>#ece9d8</BackColor></Style><Style ID="s3"><Border Left="Thin" Right="Thin" Top="Thin" Bottom="Thin"/><Alignment>CenterCenter</Alignment><Fill>True</Fill><BackColor>#c0dcc0</BackColor></Style><Style ID="s4"><Border/><Alignment>CenterCenter</Alignment><Fill>True</Fill><BackColor>#c0dcc0</BackColor></Style><Style ID="s5"><Border/><Alignment>CenterCenter</Alignment><Locked>True</Locked><Fill>True</Fill><BackColor>#ece9d8</BackColor></Style></Styles><Cells><Cell Row="0" Col="1" Tag="0,Y,N,1,0,,,"></Cell><Cell Row="0" Col="2" Tag="0,Y,Y,1,0,,,"></Cell><Cell Row="0" Col="3" Tag="0,Y,N,1,0,,,"></Cell><Cell Row="0" Col="4" Tag="0,Y,Y,1,0,,,"></Cell><Cell Row="0" Col="5" Tag="0,Y,Y,1,0,,,"></Cell><Cell Row="0" Col="6" Tag="3,Y,Y,1,0,,,"></Cell><Cell Row="1" Col="1" Font="1" Style="5">数据采集表</Cell><Cell Row="2" Col="1" Font="2" Style="2">纳税人识别号</Cell><Cell Row="2" Col="2" Font="2" Style="3" Tag="1,0,nsrsbh,,"></Cell><Cell Row="2" Col="3" Font="2" Style="2">企业名称</Cell><Cell Row="2" Col="4" Font="2" Style="3" Tag="1,0,nsrmc,,"></Cell><Cell Row="3" Col="1" Font="2" Style="2">评估方法</Cell><Cell Row="3" Col="2" Font="2" Style="3" Tag="1,0,pgffmc,,"></Cell><Cell Row="3" Col="3" Font="2" Style="2">行业类别</Cell><Cell Row="3" Col="4" Font="2" Style="3" Tag="1,0,hylb,,"></Cell><Cell Row="3" Col="5" Font="2" Style="2">纳税评估所属期</Cell><Cell Row="3" Col="6" Font="2" Style="3" Type="0" Tag="1,0,pgssq,,"></Cell><Cell Row="4" Col="1" Font="2" Style="2">项目编号</Cell><Cell Row="4" Col="2" Font="2" Style="2">评估指标</Cell><Cell Row="4" Col="3" Font="2" Style="2">评估期</Cell><Cell Row="4" Col="5" Font="2" Style="2">基期</Cell><Cell Row="5" Col="1" Font="2" Style="2">CHZ01</Cell><Cell Row="5" Col="2" Font="2" Style="2">主营业务成本</Cell><Cell Row="5" Col="3" Font="2" Style="1" Tag="1,0,{143120001},,">1000</Cell><Cell Row="5" Col="5" Font="2" Style="1"></Cell><Cell Row="6" Col="1" Font="2" Style="2">CHZ02</Cell><Cell Row="6" Col="2" Font="2" Style="2">期初业务成本</Cell><Cell Row="6" Col="3" Font="2" Style="1"></Cell><Cell Row="6" Col="5" Font="2" Style="1"></Cell><Cell Row="7" Col="1" Font="2" Style="2">CHZ03</Cell><Cell Row="7" Col="2" Font="2" Style="2">期末存货成本</Cell><Cell Row="7" Col="3" Font="2" Style="1"></Cell><Cell Row="7" Col="5" Font="2" Style="1"></Cell><Cell Row="8" Col="1" Font="2" Style="2">CHZ04</Cell><Cell Row="8" Col="2" Font="2" Style="2">应纳税额</Cell><Cell Row="8" Col="3" Font="2" Style="1"></Cell><Cell Row="8" Col="5" Font="2" Style="1"></Cell><Cell Row="9" Col="1" Font="2" Style="2">CHZ05</Cell><Cell Row="9" Col="2" Font="2" Style="2">内外销收入</Cell><Cell Row="9" Col="3" Font="2" Style="1"></Cell><Cell Row="9" Col="5" Font="2" Style="1"></Cell><Cell Row="10" Col="1" Font="2" Style="2">CHZ06</Cell><Cell Row="10" Col="2" Font="2" Style="2">进项税率</Cell><Cell Row="10" Col="3" Font="2" Style="1"></Cell><Cell Row="10" Col="5" Font="2" Style="1"></Cell><Cell Row="11" Col="1" Font="2" Style="2">CHZ07</Cell><Cell Row="11" Col="2" Font="2" Style="2">适用税率</Cell><Cell Row="11" Col="3" Font="2" Style="1"></Cell><Cell Row="11" Col="5" Font="2" Style="1"></Cell><Cell Row="12" Col="1" Font="2" Style="2">CHZ08</Cell><Cell Row="12" Col="2" Font="2" Style="2">进项税额转出</Cell><Cell Row="12" Col="3" Font="2" Style="1"></Cell><Cell Row="12" Col="5" Font="2" Style="1"></Cell><Cell Row="13" Col="1" Font="2" Style="2">CHZ09</Cell><Cell Row="13" Col="2" Font="2" Style="2">免抵退税办法出口货物不得抵扣进项</Cell><Cell Row="13" Col="3" Font="2" Style="1"></Cell><Cell Row="13" Col="5" Font="2" Style="1"></Cell><Cell Row="14" Col="1" Font="2" Style="5">评估单位</Cell><Cell Row="14" Col="2" Font="2" Style="4"></Cell><Cell Row="14" Col="3" Font="2" Style="5">评估人员</Cell><Cell Row="14" Col="4" Font="2" Style="4"></Cell><Cell Row="14" Col="5" Font="2" Style="5">评估日期</Cell><Cell Row="14" Col="6" Font="2" Style="4" Type="3" Tag="1,0,,,"></Cell></Cells><MergeCells><MergeCell Row1="1" Col1="1" Row2="1" Col2="6"/><MergeCell Row1="2" Col1="4" Row2="2" Col2="6"/><MergeCell Row1="4" Col1="3" Row2="4" Col2="4"/><MergeCell Row1="4" Col1="5" Row2="4" Col2="6"/><MergeCell Row1="5" Col1="3" Row2="5" Col2="4"/><MergeCell Row1="5" Col1="5" Row2="5" Col2="6"/><MergeCell Row1="6" Col1="3" Row2="6" Col2="4"/><MergeCell Row1="6" Col1="5" Row2="6" Col2="6"/><MergeCell Row1="7" Col1="3" Row2="7" Col2="4"/><MergeCell Row1="7" Col1="5" Row2="7" Col2="6"/><MergeCell Row1="8" Col1="3" Row2="8" Col2="4"/><MergeCell Row1="8" Col1="5" Row2="8" Col2="6"/><MergeCell Row1="9" Col1="3" Row2="9" Col2="4"/><MergeCell Row1="9" Col1="5" Row2="9" Col2="6"/><MergeCell Row1="10" Col1="3" Row2="10" Col2="4"/><MergeCell Row1="10" Col1="5" Row2="10" Col2="6"/><MergeCell Row1="11" Col1="3" Row2="11" Col2="4"/><MergeCell Row1="11" Col1="5" Row2="11" Col2="6"/><MergeCell Row1="12" Col1="3" Row2="12" Col2="4"/><MergeCell Row1="12" Col1="5" Row2="12" Col2="6"/><MergeCell Row1="13" Col1="3" Row2="13" Col2="4"/><MergeCell Row1="13" Col1="5" Row2="13" Col2="6"/></MergeCells></FlexCell>

 

 

 

package comm;

//解析FlexCell的XML内容

import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import org.xml.sax.*;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import java.util.ArrayList;

public class parseFlexCell {
 
 private Document doc;
 
 //初始化Document,将xml格式String转化成Document
 public parseFlexCell(String xmlstr)throws Exception
 {
  try{
    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
    DocumentBuilder builder=factory.newDocumentBuilder();
    StringReader sr=new StringReader(xmlstr);
    InputSource is=new InputSource(sr);
    doc=builder.parse(is);
    doc.normalize();
  }catch(Exception ex)
  {
   System.out.println("初始化Document错误");
   throw ex;
  }
 }
 
 //获取指标ArrayList
 public ArrayList getPgzbList()throws Exception
 {
  ArrayList zbarray=new ArrayList();
  try{
   NodeList cells =doc.getElementsByTagName("Cell");
   for (int i=0;i<cells.getLength();i++){
       Node cell=cells.item(i);
       Element cellattr=(Element)cells.item(i);
       if(cellattr.hasAttribute("Tag"))//判断节点有tag属性
       {
        String[] tmparray=cell.getAttributes().getNamedItem("Tag").getNodeValue().split(",");
        if(tmparray.length>2)
        {
         zbarray.add(tmparray[2]);     
        }
       }
   }
  }
  catch(Exception ex)
  {
   throw ex;
  }
  return zbarray;
 }
 
 //设置Cell值,前提没有初值
 public void setCellValue(String name,String value)throws Exception
 {
  try{
   NodeList cells =doc.getElementsByTagName("Cell");
   for (int i=0;i<cells.getLength();i++){
       Node cell=cells.item(i);
       Element cellattr=(Element)cells.item(i);
       if(cellattr.hasAttribute("Tag"))//判断节点有tag属性
       {
        String[] tmparray=cell.getAttributes().getNamedItem("Tag").getNodeValue().split(",");
        if(tmparray.length>2)
        {
         String cellname=tmparray[2];//取倒单元格表示名称
         if(cellname.equals(name))
         {
          if(!cell.hasChildNodes())//判断是否已经有值了
          {
           //创建一个值
           Text createtext = doc.createTextNode(value);
           //添加在cell后
           cell.appendChild(createtext);
          }
         }
        }
       }
   } 
  }catch(Exception ex)
  {
   throw ex;
  }
 }
 
// 读取Cell值
 public String getCellValue(String name)throws Exception
 {
  String cellvalue="";
  try{
   NodeList cells =doc.getElementsByTagName("Cell");
   for (int i=0;i<cells.getLength();i++){
       Node cell=cells.item(i);
       Element cellattr=(Element)cells.item(i);
       if(cellattr.hasAttribute("Tag"))//判断节点有tag属性
       {
        String[] tmparray=cell.getAttributes().getNamedItem("Tag").getNodeValue().split(",");
        String cellname=tmparray[2];//取倒单元格表示名称
        if(cellname.equals(name))
        {
         if(cell.hasChildNodes())//判断是否已经有值了
         {
         //返回此值
         cellvalue=cell.getFirstChild().getNodeValue(); 
         }
        } 
       }
   }
   return cellvalue;
  }catch(Exception ex)
  {
   throw ex;
  }
 }
 
 //输出Document为String
 public String toString()
    {
        String result = null;
        if (doc != null)
        {
            StringWriter strWtr = new StringWriter();
            StreamResult strResult = new StreamResult(strWtr);
            TransformerFactory tfac = TransformerFactory.newInstance();
            try
            {
                Transformer t = tfac.newTransformer();
                t.transform(new DOMSource(doc.getDocumentElement()),
                        strResult);
            }
            catch (Exception e)
            {
                System.err.println("XML.toString(Document): " + e);
            }
            result = strResult.getWriter().toString().substring(38);
        }

        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值