JAVA DOM对XML String操作

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

public class printout {
 public static void main(String [] arg)
 {
/*  String xmlstr="<root> <link>www.cxm.com</link> " +
  "<cells> " +
  "<cell tag='1,2,#nsrsbh#,'>bbb</cell> " +
  "<cell tag='1,2,3'></cell> " +
  "<cell tag='1,2,#nsrmc#,'></cell> " +
  "</cells> </root>";
  */
  String xmlstr="<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>";
  try{
   DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
   DocumentBuilder builder=factory.newDocumentBuilder();
   //将String转成parse可以识别的InputSource
   StringReader sr=new StringReader(xmlstr);
   InputSource is=new InputSource(sr);
   Document doc=builder.parse(is);
   doc.normalize();
   
   //查找cell节点
   NodeList cells =doc.getElementsByTagName("Cell");
   System.out.println("cell数目:"+cells.getLength());
   for (int i=0;i<cells.getLength();i++){
    System.out.println("循环次数:"+i);
    Node cell=cells.item(i);
    Element cellattr=(Element)cells.item(i);
    if(cellattr.hasAttribute("Tag"))//判断节点有tag属性
    {
    System.out.println(cell.getAttributes().getNamedItem("Tag").getNodeValue());
    if(cell.hasChildNodes())
    {
     System.out.println(cell.getFirstChild().getNodeValue());
    }else
    {
     //创建一个值
     Text createtext = doc.createTextNode("understand Corba");
     //添加在cell后
     cell.appendChild(createtext);
     System.out.println(cell.getFirstChild().getNodeValue());
    }
    }
   }

   //最后输出
   System.out.println(XMLtoStr(doc));
  }catch(Exception ex)
  {
   System.out.println(ex.toString());
  }
  
  
 }
 
 static public String XMLtoStr(Document document)
    {
        String result = null;

        if (document != null)
        {
            StringWriter strWtr = new StringWriter();
            StreamResult strResult = new StreamResult(strWtr);
            TransformerFactory tfac = TransformerFactory.newInstance();
            try
            {
                Transformer t = tfac.newTransformer();
                t.setOutputProperty(OutputKeys.ENCODING, "gb2312");
                t.setOutputProperty(OutputKeys.INDENT, "yes");
                t.setOutputProperty(OutputKeys.METHOD, "xml");
                t.setOutputProperty(
                        "{http://xml.apache.org/xslt}indent-amount", "4");
                t.transform(new DOMSource(document.getDocumentElement()),
                        strResult);
            }
            catch (Exception e)
            {
                System.err.println("XML.toString(Document): " + e);
            }
            result = strResult.getWriter().toString();
        }

        return result;
    }
 
 
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值