Java Code Examples for org.w3c.dom.Document#getElementById()

The following examples show how to use org.w3c.dom.Document#getElementById() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SAX2DOMTest.java    From openjdk-jdk9 with GNU General Public License v2.0 7 votes vote down vote up
@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");
    }

}
 
Example 2
Source File: ISD.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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;
}
 
Example 3
Source File: DOM2DTM.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: DOMNodePointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: BaseConfigurationService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
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);
		  }
	  }
	
  }
 
Example 6
Source File: DOM2DTM.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: SearchFieldGroup.java    From sc2gears with Apache License 2.0 5 votes vote down vote up
/**
 * 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
	}
}
 
Example 8
Source File: DOM2DTM.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: RotationTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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();
  }

}
 
Example 10
Source File: DOM2DTM.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: DOM2DTM.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: DomMatchers.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matchesSafely(Document document) {
  return document.getElementById(elementId) != null;
}
 
Example 13
Source File: IdResolver.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source File: DOM2Helper.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: IdResolver.java    From jdk8u_jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 16
Source File: IdResolver.java    From hottub with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 17
Source File: IdResolver.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: DOM2Helper.java    From jdk1.8-source-analysis with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 19
Source File: DOM2Helper.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: DOM2Helper.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}