工厂模式的具体实现类图 DocumentBuilderFactory Docum entBuilderf actor ympl1 I Mend ocurr entBuildero 定义的抽录方法 DoaumenBuiderFactorym pI2 medoc umenteuder逗回 个 DocurmentBuilde的时察 nendnstancel方法根具一个外 部的环境蹙量判断该生成的 具体的工厂类的实现 ntBuilde Document Sparse(
工厂模式的具体实现类图
DOM 解析器设置 可以控制由 DocumentBuilderFactory创建的 解析器上的各种设置 setcoalescing:确定解析器是否将 CDATA节点转成文本 并将 CDATA节点与其周围的文本节点合并。缺省值是 false set Expand Entity References:确定是否扩展外部实体引用。 如果为true,则将外部数据插入文档。缺省值是true ■ setignoringcomments:确定是否忽略文件中的注释。缺 省值是 false setIgnoringElementcontentWhitespace:确定是否忽 略元素内容中的空白。缺省值是 false setNameSpaceAware:确定解析器是否注意名称空间信息。 缺省值是 false setValidating:缺省情况下,解析器将不验证文档。将该参 数设置为tue以打开验证
DOM 解析器设置 可以控制由 DocumentBuilderFactory 创建的 解析器上的各种设置 setCoalescing:确定解析器是否将 CDATA 节点转成文本, 并将 CDATA 节点与其周围的文本节点合并。缺省值是 false。 setExpandEntityReferences:确定是否扩展外部实体引用。 如果为 true,则将外部数据插入文档。缺省值是 true setIgnoringcomments:确定是否忽略文件中的注释。缺 省值是 f l a se。 setIgnoringElementContentWhitespace:确定是否忽 略元素内容中的空白。缺省值是 false。 setNameSpaceAware:确定解析器是否注意名称空间信息。 缺省值是 false。 setValidating:缺省情况下,解析器将不验证文档。将该参 数设置为 true 以打开验证
DOM ■ A example: try t DocumentBuilderFactory dbf documentBuilderFactory newInstanceo dbf setval dating(true) DocumentBuilder cb cbf. newDocumentBuildero doc d. parse(docFile) } catch( Excepti。ne)
DOM A example: A example:
DOM ■解析器异常 为了更好地査明问题,可以捕获与创建和使用解析器各 种方面相关的特定异常 DocumentBuilderFactory dbf docwmentBuilderFactory newIns tance() DocumentBuilder db dbf. newDocumentBuilder() doc db parse(docIle catch (javax xml parsers Parser ConfigurationException pce) I System out. pr int In(The parser was not conf igured cor rectly. " System exit(1) I catch (java. io I EXception ie) I System out. pr int In( Cannot read input file " System exit(1) I catch (org. xml se) System out. pr int In(Problem parsing the file.") System exit(1) I catch (java larg. II lega gumentException ae)I System out. pr int In C Please specify an XM source. System exit(1)
DOM 解析器异常 为了更好地查明问题,可以捕获与创建和使用解析器各 种方面相关的特定异常
DOM ■遍历 旦解析完文档,并且创建了 Document之后,应用程 序可以遍历结构来复查、查找或显示信息 获取根元素:遍历文档是从根元素开始。格式良好的文档 只有一个根元素 import org. w3c dom Element Element root doc. getDocumentElemento)i System. out. println("The root element is t root. getNodeName())i
DOM 遍历 一旦解析完文档,并且创建了 Document 之后,应用程 序可以遍历结构来复查、查找或显示信息 获取根元素:遍历文档是从根元素开始。格式良好的文档 只有一个根元素 … import org.w3c.dom.Element; Element root = doc.getDocumentElement(); System.out.println("The root element is " + root getNodeName()); root.getNodeName()); …