org.w3c.dom.Document#getElementById ( )源码实例Demo

下面列出了org.w3c.dom.Document#getElementById ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-jdk9   文件: SAX2DOMTest.java
@Test
public void test() throws Exception {
    SAXParserFactory fac = SAXParserFactory.newInstance();
    fac.setNamespaceAware(true);
    SAXParser saxParser = fac.newSAXParser();

    StreamSource sr = new StreamSource(this.getClass().getResourceAsStream("SAX2DOMTest.xml"));
    InputSource is = SAXSource.sourceToInputSource(sr);
    RejectDoctypeSaxFilter rf = new RejectDoctypeSaxFilter(saxParser);
    SAXSource src = new SAXSource(rf, is);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DOMResult result = new DOMResult();
    transformer.transform(src, result);

    Document doc = (Document) result.getNode();
    System.out.println("Name" + doc.getDocumentElement().getLocalName());

    String id = "XWSSGID-11605791027261938254268";
    Element selement = doc.getElementById(id);
    if (selement == null) {
        System.out.println("getElementById returned null");
    }

}
 
源代码2 项目: ttt   文件: ISD.java
private static Collection<Element> getReferencedStyleElements(Element elt) {
    Collection<Element> elts = new java.util.ArrayList<Element>();
    if (elt.hasAttribute("style")) {
        String style = elt.getAttribute("style");
        if (!style.isEmpty()) {
            Document document = elt.getOwnerDocument();
            String[] idrefs = style.split("\\s+");
            for (String idref : idrefs) {
                Element refStyle = document.getElementById(idref);
                if (refStyle != null)
                    elts.add(refStyle);
            }
        }
    }
    return elts;
}
 
源代码3 项目: jdk1.8-source-analysis   文件: DOM2DTM.java
/**
 * Returns the <code>Element</code> whose <code>ID</code> is given by
 * <code>elementId</code>. If no such element exists, returns
 * <code>DTM.NULL</code>. Behavior is not defined if more than one element
 * has this <code>ID</code>. Attributes (including those
 * with the name "ID") are not of type ID unless so defined by DTD/Schema
 * information available to the DTM implementation.
 * Implementations that do not know whether attributes are of type ID or
 * not are expected to return <code>DTM.NULL</code>.
 *
 * <p>%REVIEW% Presumably IDs are still scoped to a single document,
 * and this operation searches only within a single document, right?
 * Wouldn't want collisions between DTMs in the same process.</p>
 *
 * @param elementId The unique <code>id</code> value for an element.
 * @return The handle of the matching element.
 */
public int getElementById(String elementId)
{

  Document doc = (m_root.getNodeType() == Node.DOCUMENT_NODE)
      ? (Document) m_root : m_root.getOwnerDocument();

  if(null != doc)
  {
    Node elem = doc.getElementById(elementId);
    if(null != elem)
    {
      int elemHandle = getHandleFromNode(elem);

      if(DTM.NULL == elemHandle)
      {
        int identity = m_nodes.size()-1;
        while (DTM.NULL != (identity = getNextNodeIdentity(identity)))
        {
          Node node = getNode(identity);
          if(node == elem)
          {
            elemHandle = getHandleFromNode(elem);
            break;
          }
         }
      }

      return elemHandle;
    }

  }
  return DTM.NULL;
}
 
源代码4 项目: openjdk-8-source   文件: DOM2DTM.java
/**
 * Returns the <code>Element</code> whose <code>ID</code> is given by
 * <code>elementId</code>. If no such element exists, returns
 * <code>DTM.NULL</code>. Behavior is not defined if more than one element
 * has this <code>ID</code>. Attributes (including those
 * with the name "ID") are not of type ID unless so defined by DTD/Schema
 * information available to the DTM implementation.
 * Implementations that do not know whether attributes are of type ID or
 * not are expected to return <code>DTM.NULL</code>.
 *
 * <p>%REVIEW% Presumably IDs are still scoped to a single document,
 * and this operation searches only within a single document, right?
 * Wouldn't want collisions between DTMs in the same process.</p>
 *
 * @param elementId The unique <code>id</code> value for an element.
 * @return The handle of the matching element.
 */
public int getElementById(String elementId)
{

  Document doc = (m_root.getNodeType() == Node.DOCUMENT_NODE)
      ? (Document) m_root : m_root.getOwnerDocument();

  if(null != doc)
  {
    Node elem = doc.getElementById(elementId);
    if(null != elem)
    {
      int elemHandle = getHandleFromNode(elem);

      if(DTM.NULL == elemHandle)
      {
        int identity = m_nodes.size()-1;
        while (DTM.NULL != (identity = getNextNodeIdentity(identity)))
        {
          Node node = getNode(identity);
          if(node == elem)
          {
            elemHandle = getHandleFromNode(elem);
            break;
          }
         }
      }

      return elemHandle;
    }

  }
  return DTM.NULL;
}
 
源代码5 项目: pentaho-reporting   文件: RotationTest.java
@Test
public void testHandleRotatedTextHTML() throws Exception {
  URL url = getClass().getResource( "BACKLOG-10064.prpt" );
  MasterReport report = (MasterReport) new ResourceManager().createDirectly( url, MasterReport.class ).getResource();
  report.getReportConfiguration().setConfigProperty( HtmlTableModule.INLINE_STYLE, "true" );
  report.getReportConfiguration().setConfigProperty( ClassicEngineCoreModule.STRICT_ERROR_HANDLING_KEY, "false" );

  List<String> elementsIdList = Arrays.asList("topLeft90","topCenter90","topRight90","topJustify90",
                                              "topLeft-90","topCenter-90","topRight-90","topJustify-90",
                                              "middleLeft90","middleCenter90","middleRight90","middleJustify90",
                                              "middleLeft-90","middleCenter-90","middleRight-90","middleJustify-90",
                                              "bottomLeft90","bottomCenter90","bottomRight90","bottomJustify90",
                                              "bottomLeft-90","bottomCenter-90","bottomRight-90","bottomJustify-90");

  XPathFactory xpathFactory = XPathFactory.newInstance();
  try ( ByteArrayOutputStream stream = new ByteArrayOutputStream() ) {
    HtmlReportUtil.createStreamHTML( report, stream );
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware( true );
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse( new ByteArrayInputStream( stream.toByteArray() ) );

    for ( String elementId : elementsIdList ) {
      org.w3c.dom.Element element = document.getElementById( elementId );
      Node rotatedTextStyle = element.getFirstChild().getAttributes().getNamedItem( "style" );
      Node trSryle = element.getParentNode().getParentNode().getAttributes().getNamedItem( "style" );
      assertTrue( isStyleValid( rotatedTextStyle.getNodeValue(), trSryle.getNodeValue(), elementId.contains( "middle" ) ) );
    }
  } catch ( final IOException | ReportProcessingException e ) {
    fail();
  }

}
 
源代码6 项目: j2objc   文件: DOM2DTM.java
/**
 * Returns the <code>Element</code> whose <code>ID</code> is given by
 * <code>elementId</code>. If no such element exists, returns
 * <code>DTM.NULL</code>. Behavior is not defined if more than one element
 * has this <code>ID</code>. Attributes (including those
 * with the name "ID") are not of type ID unless so defined by DTD/Schema
 * information available to the DTM implementation.
 * Implementations that do not know whether attributes are of type ID or
 * not are expected to return <code>DTM.NULL</code>.
 *
 * <p>%REVIEW% Presumably IDs are still scoped to a single document,
 * and this operation searches only within a single document, right?
 * Wouldn't want collisions between DTMs in the same process.</p>
 *
 * @param elementId The unique <code>id</code> value for an element.
 * @return The handle of the matching element.
 */
public int getElementById(String elementId)
{

  Document doc = (m_root.getNodeType() == Node.DOCUMENT_NODE) 
      ? (Document) m_root : m_root.getOwnerDocument();
      
  if(null != doc)
  {
    Node elem = doc.getElementById(elementId);
    if(null != elem)
    {
      int elemHandle = getHandleFromNode(elem);
      
      if(DTM.NULL == elemHandle)
      {
        int identity = m_nodes.size()-1;
        while (DTM.NULL != (identity = getNextNodeIdentity(identity)))
        {
          Node node = getNode(identity);
          if(node == elem)
          {
            elemHandle = getHandleFromNode(elem);
            break;
          }
         }
      }
      
      return elemHandle;
    }
  
  }
  return DTM.NULL;
}
 
源代码7 项目: sc2gears   文件: SearchFieldGroup.java
/**
 * Loads the saved value from the specified document.
 * @param document document to load the value from
 * @param subId    sub-id to differentiate between multiple instances of this search field
 */
public void loadValues( final Document document ) {
	final Element filterGroupElement = document.getElementById( searchFieldList.get( 0 ).id.name() );
	
	boolean filterGroupMissing = true;
	
	if ( filterGroupElement != null ) {
		final NodeList filterElementList = filterGroupElement.getElementsByTagName( "filter" );
		
		final int filtersCount = filterElementList.getLength();
		if ( filtersCount > 0 ) {
			ensureExactSearchFieldsCount( filtersCount );
			
			for ( int i = 0; i < filtersCount; i++ ) {
				final SearchField searchField = searchFieldList.get( i );
				searchField.reset(); // To clear previous errors
				searchField.loadValue( (Element) filterElementList.item( i ) );
			}
			
			filterGroupMissing = false;
		}
	}
	
	if ( filterGroupMissing ) {
		ensureExactSearchFieldsCount( 1 );
		searchFieldList.get( 0 ).reset(); // To clear previous errors
	}
}
 
源代码8 项目: openjdk-jdk8u-backup   文件: DOM2DTM.java
/**
 * Returns the <code>Element</code> whose <code>ID</code> is given by
 * <code>elementId</code>. If no such element exists, returns
 * <code>DTM.NULL</code>. Behavior is not defined if more than one element
 * has this <code>ID</code>. Attributes (including those
 * with the name "ID") are not of type ID unless so defined by DTD/Schema
 * information available to the DTM implementation.
 * Implementations that do not know whether attributes are of type ID or
 * not are expected to return <code>DTM.NULL</code>.
 *
 * <p>%REVIEW% Presumably IDs are still scoped to a single document,
 * and this operation searches only within a single document, right?
 * Wouldn't want collisions between DTMs in the same process.</p>
 *
 * @param elementId The unique <code>id</code> value for an element.
 * @return The handle of the matching element.
 */
public int getElementById(String elementId)
{

  Document doc = (m_root.getNodeType() == Node.DOCUMENT_NODE)
      ? (Document) m_root : m_root.getOwnerDocument();

  if(null != doc)
  {
    Node elem = doc.getElementById(elementId);
    if(null != elem)
    {
      int elemHandle = getHandleFromNode(elem);

      if(DTM.NULL == elemHandle)
      {
        int identity = m_nodes.size()-1;
        while (DTM.NULL != (identity = getNextNodeIdentity(identity)))
        {
          Node node = getNode(identity);
          if(node == elem)
          {
            elemHandle = getHandleFromNode(elem);
            break;
          }
         }
      }

      return elemHandle;
    }

  }
  return DTM.NULL;
}
 
源代码9 项目: sakai   文件: BaseConfigurationService.java
protected void saveServletClientMappings(Document document) {
	  
	  Element clientElement = document.getElementById("saveciteClients");
	  
	  if(clientElement == null) {
		  NodeList mapNodes = document.getElementsByTagName("map");
		  if(mapNodes != null) {
			  for(int i = 0; i < mapNodes.getLength(); i++) {
				  Element mapElement = (Element) mapNodes.item(i);
				  if(mapElement.hasAttribute("id") && mapElement.getAttribute("id").equals("saveciteClients")) {
					  clientElement = mapElement;
					  break;
				  }
			  }
		  }
	  }
	  
	  if(clientElement != null) {
		  try {
			  XStream xstream = new XStream();
			  TransformerFactory transFactory = TransformerFactory.newInstance();
			  Transformer transformer = transFactory.newTransformer();
			  StringWriter buffer = new StringWriter();
			  transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			  transformer.transform(new DOMSource(clientElement),
			        new StreamResult(buffer));
			  String str = buffer.toString();
//			  DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
//			  LSSerializer serializer = domImplLS.createLSSerializer();
//			  String str = serializer.writeToString(clientElement);
			  this.saveciteClients = (Map<String, List<Map<String, String>>>) xstream.fromXML(str);
		  } catch(Exception e) {
			  log.warn("Exception trying to read saveciteClients from config XML", e);
		  }
	  }
	
  }
 
源代码10 项目: commons-jxpath   文件: DOMNodePointer.java
/**
 * Locates a node by ID.
 * @param context starting context
 * @param id to find
 * @return Pointer
 */
public Pointer getPointerByID(JXPathContext context, String id) {
    Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node
            : node.getOwnerDocument();
    Element element = document.getElementById(id);
    return element == null ? (Pointer) new NullPointer(getLocale(), id)
            : new DOMNodePointer(element, getLocale(), id);
}
 
源代码11 项目: openjdk-jdk9   文件: DOM2DTM.java
/**
 * Returns the <code>Element</code> whose <code>ID</code> is given by
 * <code>elementId</code>. If no such element exists, returns
 * <code>DTM.NULL</code>. Behavior is not defined if more than one element
 * has this <code>ID</code>. Attributes (including those
 * with the name "ID") are not of type ID unless so defined by DTD/Schema
 * information available to the DTM implementation.
 * Implementations that do not know whether attributes are of type ID or
 * not are expected to return <code>DTM.NULL</code>.
 *
 * <p>%REVIEW% Presumably IDs are still scoped to a single document,
 * and this operation searches only within a single document, right?
 * Wouldn't want collisions between DTMs in the same process.</p>
 *
 * @param elementId The unique <code>id</code> value for an element.
 * @return The handle of the matching element.
 */
public int getElementById(String elementId)
{

  Document doc = (m_root.getNodeType() == Node.DOCUMENT_NODE)
      ? (Document) m_root : m_root.getOwnerDocument();

  if(null != doc)
  {
    Node elem = doc.getElementById(elementId);
    if(null != elem)
    {
      int elemHandle = getHandleFromNode(elem);

      if(DTM.NULL == elemHandle)
      {
        int identity = m_nodes.size()-1;
        while (DTM.NULL != (identity = getNextNodeIdentity(identity)))
        {
          Node node = getNode(identity);
          if(node == elem)
          {
            elemHandle = getHandleFromNode(elem);
            break;
          }
         }
      }

      return elemHandle;
    }

  }
  return DTM.NULL;
}
 
源代码12 项目: android-test   文件: DomMatchers.java
@Override
public boolean matchesSafely(Document document) {
  return document.getElementById(elementId) != null;
}
 
源代码13 项目: openjdk-8-source   文件: DOM2Helper.java
/**
 * Given an XML ID, return the element. This requires assistance from the
 * DOM and parser, and is meaningful only in the context of a DTD
 * or schema which declares attributes as being of type ID. This
 * information may or may not be available in all parsers, may or
 * may not be available for specific documents, and may or may not
 * be available when validation is not turned on.
 *
 * @param id The ID to search for, as a String.
 * @param doc The document to search within, as a DOM Document node.
 * @return DOM Element node with an attribute of type ID whose value
 * uniquely matches the requested id string, or null if there isn't
 * such an element or if the DOM can't answer the question for other
 * reasons.
 */
public Element getElementByID(String id, Document doc)
{
  return doc.getElementById(id);
}
 
源代码14 项目: jdk1.8-source-analysis   文件: DOM2Helper.java
/**
 * Given an XML ID, return the element. This requires assistance from the
 * DOM and parser, and is meaningful only in the context of a DTD
 * or schema which declares attributes as being of type ID. This
 * information may or may not be available in all parsers, may or
 * may not be available for specific documents, and may or may not
 * be available when validation is not turned on.
 *
 * @param id The ID to search for, as a String.
 * @param doc The document to search within, as a DOM Document node.
 * @return DOM Element node with an attribute of type ID whose value
 * uniquely matches the requested id string, or null if there isn't
 * such an element or if the DOM can't answer the question for other
 * reasons.
 */
public Element getElementByID(String id, Document doc)
{
  return doc.getElementById(id);
}
 
源代码15 项目: j2objc   文件: DOM2Helper.java
/**
 * Given an XML ID, return the element. This requires assistance from the
 * DOM and parser, and is meaningful only in the context of a DTD 
 * or schema which declares attributes as being of type ID. This
 * information may or may not be available in all parsers, may or
 * may not be available for specific documents, and may or may not
 * be available when validation is not turned on.
 *
 * @param id The ID to search for, as a String.
 * @param doc The document to search within, as a DOM Document node.
 * @return DOM Element node with an attribute of type ID whose value
 * uniquely matches the requested id string, or null if there isn't
 * such an element or if the DOM can't answer the question for other
 * reasons.
 */
public Element getElementByID(String id, Document doc)
{
  return doc.getElementById(id);
}
 
源代码16 项目: jdk8u60   文件: IdResolver.java
/**
 * Method getElementById
 *
 * @param doc the document
 * @param id the value of the ID
 * @return the element obtained by the id, or null if it is not found.
 */
public static Element getElementById(Document doc, String id) {
    return doc.getElementById(id);
}
 
源代码17 项目: jdk8u_jdk   文件: IdResolver.java
/**
 * Method getElementById
 *
 * @param doc the document
 * @param id the value of the ID
 * @return the element obtained by the id, or null if it is not found.
 */
public static Element getElementById(Document doc, String id) {
    return doc.getElementById(id);
}
 
源代码18 项目: hottub   文件: IdResolver.java
/**
 * Method getElementById
 *
 * @param doc the document
 * @param id the value of the ID
 * @return the element obtained by the id, or null if it is not found.
 */
public static Element getElementById(Document doc, String id) {
    return doc.getElementById(id);
}
 
源代码19 项目: openjdk-8   文件: IdResolver.java
/**
 * Method getElementById
 *
 * @param doc the document
 * @param id the value of the ID
 * @return the element obtained by the id, or null if it is not found.
 */
public static Element getElementById(Document doc, String id) {
    return doc.getElementById(id);
}
 
源代码20 项目: openjdk-jdk9   文件: DOM2Helper.java
/**
 * Given an XML ID, return the element. This requires assistance from the
 * DOM and parser, and is meaningful only in the context of a DTD
 * or schema which declares attributes as being of type ID. This
 * information may or may not be available in all parsers, may or
 * may not be available for specific documents, and may or may not
 * be available when validation is not turned on.
 *
 * @param id The ID to search for, as a String.
 * @param doc The document to search within, as a DOM Document node.
 * @return DOM Element node with an attribute of type ID whose value
 * uniquely matches the requested id string, or null if there isn't
 * such an element or if the DOM can't answer the question for other
 * reasons.
 */
public Element getElementByID(String id, Document doc)
{
  return doc.getElementById(id);
}