コード例 #1
0
        /// <summary>
        /// Parse a fragment of HTML into the
        /// <c>body</c>
        /// of a Document.
        /// </summary>
        /// <param name="bodyHtml">fragment of HTML</param>
        /// <param name="baseUri">base URI of document (i.e. original fetch location), for resolving relative URLs.</param>
        /// <returns>Document, with empty head, and HTML parsed into body</returns>
        public static Document ParseBodyFragment(String bodyHtml, String baseUri)
        {
            Document doc = Document.CreateShell(baseUri);

            iText.StyledXmlParser.Jsoup.Nodes.Element      body     = doc.Body();
            IList <iText.StyledXmlParser.Jsoup.Nodes.Node> nodeList = ParseFragment(bodyHtml, body, baseUri);

            iText.StyledXmlParser.Jsoup.Nodes.Node[] nodes = nodeList.ToArray(new iText.StyledXmlParser.Jsoup.Nodes.Node
                                                                              [nodeList.Count]);
            // the node list gets modified when re-parented
            for (int i = nodes.Length - 1; i > 0; i--)
            {
                nodes[i].Remove();
            }
            foreach (iText.StyledXmlParser.Jsoup.Nodes.Node node in nodes)
            {
                body.AppendChild(node);
            }
            return(doc);
        }