<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    zhyiwww
    用平實(shí)的筆,記錄編程路上的點(diǎn)點(diǎn)滴滴………
    posts - 536,comments - 394,trackbacks - 0

    Parsing XML

    One of the first things you'll probably want to do is to parse an XML document of some kind. This is easy to do in dom4j. The following code demonstrates how to this.

    import java.net.URL;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.io.SAXReader;
    
    public class Foo {
    
        public Document parse(URL url) throws DocumentException {
            SAXReader reader = new SAXReader();
            Document document = reader.read(url);
            return document;
        }
    }
    

    Using Iterators

    A document can be navigated using a variety of methods that return standard Java Iterators. For example

        public void bar(Document document) throws DocumentException {
    
            Element root = document.getRootElement();
    
            // iterate through child elements of root
            for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
                Element element = (Element) i.next();
                // do something
            }
    
            // iterate through child elements of root with element name "foo"
            for ( Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) {
                Element foo = (Element) i.next();
                // do something
            }
    
            // iterate through attributes of root 
            for ( Iterator i = root.attributeIterator(); i.hasNext(); ) {
                Attribute attribute = (Attribute) i.next();
                // do something
            }
         }
    

    Powerful Navigation with XPath

    In dom4j XPath expressions can be evaluated on the Document or on any Node in the tree (such as Attribute, Element or ProcessingInstruction). This allows complex navigation throughout the document with a single line of code. For example.

        public void bar(Document document) {
    ????????
    ???????//Get the value of the node using the XPath List list = document.selectNodes( "http://foo/bar" ); Node node = document.selectSingleNode( "http://foo/bar/author" ); String name = node.valueOf( "@name" ); }

    For example if you wish to find all the hypertext links in an XHTML document the following code would do the trick.

        public void findLinks(Document document) throws DocumentException {
    
            List list = document.selectNodes( "http://a/@href" );
    
            for (Iterator iter = list.iterator(); iter.hasNext(); ) {
                Attribute attribute = (Attribute) iter.next();
                String url = attribute.getValue();
            }
        }
    

    If you need any help learning the XPath language we highly recommend the Zvon tutorial which allows you to learn by example.

    Fast Looping

    If you ever have to walk a large XML document tree then for performance we recommend you use the fast looping method which avoids the cost of creating an Iterator object for each loop. For example

        public void treeWalk(Document document) {
            treeWalk( document.getRootElement() );
        }
    
        public void treeWalk(Element element) {
            for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
                Node node = element.node(i);
                if ( node instanceof Element ) {
                    treeWalk( (Element) node );
                }
                else {
                    // do something....
                }
            }
        }
    

    Creating a new XML document

    Often in dom4j you will need to create a new document from scratch. Here's an example of doing that.

    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    
    public class Foo {
    
        public Document createDocument() {
            Document document = DocumentHelper.createDocument();
            Element root = document.addElement( "root" );
    
            Element author1 = root.addElement( "author" )
                .addAttribute( "name", "James" )
                .addAttribute( "location", "UK" )
                .addText( "James Strachan" );
            
            Element author2 = root.addElement( "author" )
                .addAttribute( "name", "Bob" )
                .addAttribute( "location", "US" )
                .addText( "Bob McWhirter" );
    
            return document;
        }
    }
    

    Writing a document to a file

    A quick and easy way to write a Document (or any Node) to a Writer is via the write() method.

      FileWriter out = new FileWriter( "foo.xml" );
      document.write( out );
    

    If you want to be able to change the format of the output, such as pretty printing or a compact format, or you want to be able to work with Writer objects or OutputStream objects as the destination, then you can use the XMLWriter class.

    import org.dom4j.Document;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;
    
    public class Foo {
    
        public void write(Document document) throws IOException {
    
            // lets write to a file
            XMLWriter writer = new XMLWriter(
                new FileWriter( "output.xml" )
            );
            writer.write( document );
            writer.close();
    
    
            // Pretty print the document to System.out
            OutputFormat format = OutputFormat.createPrettyPrint();
            writer = new XMLWriter( System.out, format );
            writer.write( document );
    
            // Compact format to System.out
            format = OutputFormat.createCompactFormat();
            writer = new XMLWriter( System.out, format );
            writer.write( document );
        }
    }
    

    Converting to and from Strings

    If you have a reference to a Document or any other Node such as an Attribute or Element, you can turn it into the default XML text via the asXML() method.

            Document document = ...;
            String text = document.asXML();
    

    If you have some XML as a String you can parse it back into a Document again using the helper method DocumentHelper.parseText()

            String text = "<person> <name>James</name> </person>";
            Document document = DocumentHelper.parseText(text);
    

    Styling a Document with XSLT

    Applying XSLT on a Document is quite straightforward using the JAXP API from Sun. This allows you to work against any XSLT engine such as Xalan or SAXON. Here is an example of using JAXP to create a transformer and then applying it to a Document.

    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    
    import org.dom4j.Document;
    import org.dom4j.io.DocumentResult;
    import org.dom4j.io.DocumentSource;
    
    public class Foo {
    
        public Document styleDocument(
            Document document, 
            String stylesheet
        ) throws Exception {
    
            // load the transformer using JAXP
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer( 
                new StreamSource( stylesheet ) 
            );
    
            // now lets style the given document
            DocumentSource source = new DocumentSource( document );
            DocumentResult result = new DocumentResult();
            transformer.transform( source, result );
    
            // return the transformed document
            Document transformedDoc = result.getDocument();
            return transformedDoc;
        }
    }
    


    |----------------------------------------------------------------------------------------|
                               版權(quán)聲明  版權(quán)所有 @zhyiwww
                引用請(qǐng)注明來源 http://m.tkk7.com/zhyiwww   
    |----------------------------------------------------------------------------------------|
    posted on 2006-10-24 19:26 zhyiwww 閱讀(1514) 評(píng)論(0)  編輯  收藏 所屬分類: j2ee
    主站蜘蛛池模板: 亚洲欧洲日韩国产| 无码毛片一区二区三区视频免费播放 | 久久久免费观成人影院| 91大神在线免费观看| 亚洲伦乱亚洲h视频| 在线a亚洲老鸭窝天堂av高清| 亚洲最大福利视频网站| 午夜亚洲乱码伦小说区69堂| 在线看片韩国免费人成视频| 西西人体44rt高清亚洲| 久久国产一片免费观看| 亚洲黄色网站视频| 日韩精品人妻系列无码专区免费| 成人性生免费视频| 亚洲精品自拍视频| 日本高清在线免费| 天天爽亚洲中文字幕| 亚洲黄色免费网站| 亚洲国产精品成人综合久久久| 最近的2019免费中文字幕| 国产免费人成在线视频| 亚洲色少妇熟女11p| 亚洲va久久久噜噜噜久久 | 皇色在线免费视频| 亚洲av无码片在线观看| 成人女人A级毛片免费软件| 国产亚洲精品bv在线观看| 成人免费在线视频| 99久久精品免费视频| 成人a毛片免费视频观看| 亚洲一区在线视频| 亚洲自偷自拍另类12p| 亚洲伊人久久综合影院| 精品四虎免费观看国产高清午夜| 无码国产精品久久一区免费| 国产成人免费AV在线播放 | 久久不见久久见免费影院www日本| 中文字幕亚洲综合久久菠萝蜜| 亚洲成AV人综合在线观看| 亚洲精品乱码久久久久久按摩| 亚洲AV无码专区国产乱码不卡|