这里面包含了一百多个JAVA源文件

源代码在线查看: e529. getting the declared entities in a dom document.txt

软件大小: 551 K
上传用户: maple_78
关键词: JAVA
下载地址: 免注册下载 普通下载 VIP

相关代码

				The entities declared in the DTD of an XML document are available in the DOM document. Unfortunately, with J2SE 1.4's default parser, only the names, not the values, are available. In order to obtain the values, you must parse the file without expanding entity references and then scan the DOM document for the unexpanded entity references. The unexpanded entity references contain the values. 
				Note: By default, a parser expands entity references while constructing the DOM tree. See e516 Preventing Expansion of Entity References While Parsing an XML File to prevent expansion. The default parser in J2SE 1.4 expands entity references in attribute values. There is no way to prevent this. 
				
				    // Obtain a document; this method is implemented in
				    // e516 Preventing Expansion of Entity References While Parsing an XML File
				    Document doc = parseXmlFileNoExpandER("infilename.xml", true);
				    
				    // Scan the document for entity references and get their values.
				    // The values are stored in the map using the entity name as the key.
				    Map entityValues = new HashMap();
				    getEntityValues(doc, entityValues);
				    
				    // Get list of declared entities
				    NamedNodeMap entities = doc.getDoctype().getEntities();
				    for (int i=0; i				        Entity entity = (Entity)entities.item(i);
				        String entityName = entity.getNodeName();
				        String entityPublicId = entity.getPublicId();
				        String entitySystemId = entity.getSystemId();
				    
				        // Get the value of the entity, which is its set of child nodes
				        Node entityValue = (Node)entityValues.get(entityName);
				    }
				    
				    // This method walks the document looking for entity references.
				    // When one is found, this method adds the entity reference node
				    // to `map' using the name as the key.
				    public static void getEntityValues(Node node, Map map) {
				        if (node instanceof EntityReference) {
				            map.put(node.getNodeName(), node);
				        }
				    
				        // Visit the children
				        NodeList list = node.getChildNodes();
				        for (int i=0; i				            getEntityValues(list.item(i), map);
				        }
				    }
				
				This is the sample input for the example: 
				    
				    				        
				    
				        
				        
				    
				        
				        
				    
				        				             another file. It will not appear in the DOM document. However,
				             any non-parameter entities declared in the file will be included. -->
				        
				        %entity4;
				    
				        
				        
				        
				        
				        
				    ]>
				    
				        &entity1;
				        &entity3;
				        &ent1;
				        &ent2;
				        
				    
				
				External.xml: 
				    
				    external text
				
				More.dtd: 
				    
				    
				    
				    
				
				The following lists the entities that would appear in the DOM document. Their values are also listed. Notice the parameter entities entity2 and entity4 do not appear in the list. 
				    entity1=an internal entity
				    entity3=external text
				    ent1=xx
				    ent2=yy
				    ent5=null
				
				If the input file were parsed with entity expansion, the resulting XML would be: 
				    
				    
				        an internal entity
				        
				    external text
				    
				        xx
				        yy
				        
				    
				
				Note: The J2SE 1.4 DOM writing routines don't appear to write entity references properly. In particular, only text nodes that are descendants of the entity reference are written; all other node types are simply not printed. 			

相关资源