请教各位高手:有这样一个tpl-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<!--first templete -->
   <tpl index="index">
      <tlpPath >templete/index.html</tlpPath>
      <tlpParameter tlpParameter="tlpParameterppp">
         <area>###index1###</area>
         <infoId>45</infoId>
         <display>6</display>
         <date>0</date>
         <char>0</char>
      </tlpParameter>
      
      
      <tlpParameter>
         <area>###index2###</area>
         <infoId>45</infoId>
         <display>6</display>
         <date>0</date>
         <char>0</char>
      </tlpParameter>
   </tpl><!--second templete -->   
   <tpl zk="zk">
      <tlpPath>templetezk/zk.html</tlpPath>
      <tlpPatameter>
         <area>###zk1###</area>
         <display>10</display>
      </tlpPatameter>
      
      
      <tlpPatameter>
         <area>###zk2###</area>
         <display>8</display>
      </tlpPatameter>
   </tpl><!--three templete -->
<tpl ck="ck">
      <tlpPath>templeteck/ck.html</tlpPath>
      <tlpPatameter>
         <area>###ck1###</area>
         <display>10</display>
      </tlpPatameter>
      
      
      <tlpPatameter>
         <area>###ck2###</area>
         <display>8</display>
      </tlpPatameter>
   </tpl>      
</xml-body>
怎样输出各节点的名字和值???
就像:
index== index
tlpPath==templete/index.htmlarea==###index1###
infoId==45
display==6
date==0
char==0area==###index2###
infoId==45
display==6
date==0
char==0zk===zk
tlpPath==templetezk/zk.html
...循环输出全部,解决立刻给分。

解决方案 »

  1.   

    package hrsys.in;import java.io.File;
    import java.io.IOException;import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;public class SAXPrinterTest extends DefaultHandler{

    public void startDocument() throws SAXException {
    System.out.println("<?xml version=\"1.0\" encoding=\"gbk\"?>");
    }
    public void processingInstruction(String taget, String data) throws SAXException {
    System.out.println("<?"+taget+" " + data+ "?>");
    }

    public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException {
    System.out.print("<" + qName);
    for(int i = 0;i<attr.getLength();i++)
    {
    System.out.print(" ");
    System.out.print(attr.getQName(i));
    System.out.print("=\"");
    System.out.print(attr.getValue(i));
    System.out.print("\"");
    }

    System.out.print(">");
    }
    public void characters(char[] ch, int start, int length) throws SAXException {
    System.out.print(new String(ch,start,length));
    }
    public void endElement(String uri, String localName, String qName) throws SAXException {
    System.out.print("<" + qName+"/>");
    }

    public static void main(String args[])
    {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
    SAXParser sp = spf.newSAXParser();
    sp.parse(new File("c:/exp/friend.xml"),new SAXPrinterTest());
    } catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
      

  2.   

    不能循环吧
    <tpl index="index">
    <tpl zk="zk">
    <tpl ck="ck">
    中的属性名是要给出的
      

  3.   

    解析xml我觉得还是dom4j方便些
      

  4.   

    楼上的对dom4j解析50M以上大文件有研究吗??
      

  5.   

    谢谢dilaomimi()!
    今天一直上不了网!
    现在给分!