//saxtest.javaimport org.xml.sax.*;
import org.apache.xerces.parsers.*;
import org.xml.sax.helpers.*;public class saxtest extends DefaultHandler{
public static void main(String args[])throws Exception{
(new saxtest()).run(args[0]);
}

public void run(String file)throws Exception{
XMLReader parser=new SAXParser();
parser.setContentHandler(this);
parser.parse(file);
}

public void startDocument() throws SAXException{
System.out.println("starting parse XML file....");
}

public void startElement(String uri, String localName, String rawName,Attributes attlist) throws SAXException {
System.out.println(localName);
}

public void endElement(String uri, String localName, String rawName) throws SAXException {
}

public void characters(char[] ch,int start,int length) throws SAXException{
System.out.println(new String(ch,start,length));

}

public void endDocument() throws SAXException{
System.out.println("end parse XML file!");
}
}

解决方案 »

  1.   

    //domtest.javaimport javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import java.io.File;
    import org.w3c.dom.*;public class domtest{
       public static void main (String args[]) {
          File docFile = new File(args[0]);
          Document doc = null;      
          try {  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
             doc = db.parse(docFile);
             Element root=doc.getDocumentElement();
             System.out.println(root.getNodeName());
             NodeList rootnl=root.getChildNodes();
             for(int i=0;i<rootnl.getLength();i++){
              Node currentNode=rootnl.item(i);
              if(currentNode.getNodeName().equals("fo:layout-master-set")){
              System.out.println(currentNode.getNodeName());
              }else if(currentNode.getNodeName().equals("fo:page-sequence")){
              System.out.println(currentNode.getNodeName());
              }else{
              System.out.println("Unknow NodeName:"+currentNode.getNodeName());
              }
             }

          } catch (Exception e) {
             System.out.print("Problem parsing the file.");
          }
       }
    }
      

  2.   

    package test.dom;import java.lang.System;
    import java.io.StringWriter;
    import java.io.PrintWriter;
    import java.io.FileWriter;
    import org.apache.xerces.dom.DocumentImpl;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.apache.xml.serialize.OutputFormat;
    import org.apache.xml.serialize.XMLSerializer;public class CreateDom
    {
    public static final void main( String[] argv ) throws Exception
    {
    Document dom = new DocumentImpl(); Element eRoot = dom.createElement( "root" );
    dom.appendChild( eRoot ); OutputFormat format = new OutputFormat( dom );
    StringWriter stringOut = new StringWriter();
            XMLSerializer serial = new XMLSerializer( stringOut, format );        serial.serialize( dom );        System.out.println( "STRXML = \n" + stringOut.toString() ); PrintWriter ps = new PrintWriter( new FileWriter( "d:\\productinfo.xml" ) );
    ps.write( stringOut.toString() );
    ps.close();
    }
    }
      

  3.   

    我用jbuilder7编译domtest.java时他说:java.lang.ArrayIndexOutOfBoundsException at domtest.domtest.main(domtest.java:10)Exception in thread "main" 
    请问这是什么道理?另外,File docFile = new File(args[0]);
    这句话什么意思?
      

  4.   

    1。你所看多的不是编译错误,编译已经通过,是运行错误!
    你这样设置一下Run/Configurations/Run/选中你的哪个Untitled Runtime Configuration点编辑。
    设置Application arameters:
    E:\hello.fo(你自己改!)
    OK!
    2.File docFile = new File(args[0]);
    根据给定的文件路径,建立一个File对象,具体参考Java文挡.
    3.我的程序是用来测试.fo文件的(一种XML文件)
    下面是 hello.fo
    <?xml version="1.0" encoding="utf-8" ?> 
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="simple">
       <fo:region-body/> 
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="simple">
        <fo:flow flow-name="xsl-region-body">
    <fo:block>Hello World!</fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>Good luck! 
    ----------------------------------------------------------------
    原贴内容:
    我用jbuilder7编译domtest时他说:java.lang.ArrayIndexOutOfBoundsException at domtest.domtest.main(domtest.java:10)Exception in thread "main" 
    请问这是什么道理?另外,File docFile = new File(args[0]);
    这句话什么意思?
      

  5.   

    你这个程序是针对特定的xml文件的,能不能给我一个针对任意xml文件的程序?
    另外, 麻烦你能不能给我解释一下这段程序,我看大不明白,谢谢。
      

  6.   

    你这个程序是针对特定的xml文件的,能不能给我一个针对任意xml文件的程序?