下面列出了org.w3c.dom.Document#getElementById ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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");
}
}
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;
}
/**
* 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;
}
/**
* 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;
}
@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();
}
}
/**
* 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;
}
/**
* 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
}
}
/**
* 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;
}
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);
}
}
}
/**
* 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);
}
/**
* 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;
}
@Override
public boolean matchesSafely(Document document) {
return document.getElementById(elementId) != null;
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}