Java Code Examples for org.w3c.dom.Element#getLastChild()

The following examples show how to use org.w3c.dom.Element#getLastChild() . 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: DOMBuilder.java    From symja_android_library with GNU General Public License v3.0 6 votes vote down vote up
public Node appendTextNode(Element parentElement, String content, boolean trim) {
    String toAppend = trim ? content.trim() : content;
    toAppend = toAppend.replace(lineSeparator, "\n"); /* (Normalise line endings) */
    
    /* We'll coalesce adjacent text Nodes */
    Node lastChild = parentElement.getLastChild();
    if (lastChild!=null && lastChild.getNodeType()==Node.TEXT_NODE) {
        lastChild.setNodeValue(lastChild.getNodeValue() + toAppend);
    }
    else {
        /* Previous sibling is not text Node so create new one */
        lastChild = document.createTextNode(toAppend);
        parentElement.appendChild(lastChild);
    }
    return lastChild;
}
 
Example 2
Source File: XMLUtil.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static Element getLastChild(Element e) {
  if (e == null)
    return null;
  Node n = e.getLastChild();
  while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
    n = n.getPreviousSibling();
  return (Element) n;
}
 
Example 3
Source File: ElementTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGetChild() throws Exception {
    Document document = createDOMWithNS("ElementSample01.xml");
    Element elemNode = (Element) document.getElementsByTagName("b:aaa").item(0);
    elemNode.normalize();
    Node firstChild = elemNode.getFirstChild();
    Node lastChild = elemNode.getLastChild();
    assertEquals(firstChild.getNodeValue(), "fjfjf");
    assertEquals(lastChild.getNodeValue(), "fjfjf");
}
 
Example 4
Source File: PlainTextParser.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Parses the character stream to get the DOM tree.
 * 
 * @param reader
 *            the Reader for providing the character stream
 * @return DOM tree whose top element node is named "body" if no error
 *         exists, otherwise null.
 */
private Document parsePlainText( Reader reader )
{
	try
	{
		Document doc = DocumentBuilderFactory.newInstance( )
				.newDocumentBuilder( ).newDocument( );
		Element body = doc.createElement( "body" ); //$NON-NLS-1$
		doc.appendChild( body );

		LineNumberReader lReader = new LineNumberReader( reader );
		String content;
		//Only the control code that formats the layout is done here and
		// others are preserved without change to be passed on to Emitter
		// for processing.			
		while ( ( content = lReader.readLine( ) ) != null )
		{
			appendChild( doc, body, content );
			body.appendChild( doc.createElement( "br" ) ); //$NON-NLS-1$
		}
		//Removes the last BR element.
		//TODO The last BR element is removed even if there is a CR/LF
		// control code at the end of the file.
		if ( body.getLastChild( ) != null )
		{
			body.removeChild( body.getLastChild( ) );
		}
		return doc;
	}
	catch ( Exception e )
	{
	    logger.log( Level.SEVERE, e.getMessage(),  e );
	}
	return null;
}
 
Example 5
Source File: MAPCodec.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void encodeReferenceParameters(AddressingProperties maps,
                                       SoapMessage msg,
                                       JAXBContext ctx) throws JAXBException {
    Element header = null;

    EndpointReferenceType toEpr = maps.getToEndpointReference();
    if (null != toEpr) {
        ReferenceParametersType params = toEpr.getReferenceParameters();
        if (null != params) {
            for (Object o : params.getAny()) {
                if (o instanceof Element || o instanceof JAXBElement) {
                    if (header == null) {
                        header = getHeaderFactory().getHeader(msg.getVersion());
                    }
                    JAXBElement<?> jaxbEl = null;
                    if (o instanceof Element) {
                        Element e = (Element)o;
                        Node importedNode = header.getOwnerDocument().importNode(e, true);
                        header.appendChild(importedNode);
                    } else {
                        jaxbEl = (JAXBElement<?>) o;
                        ctx.createMarshaller().marshal(jaxbEl, header);
                    }

                    Element lastAdded = (Element)header.getLastChild();
                    header.removeChild(lastAdded);
                    addIsReferenceParameterMarkerAttribute(lastAdded, maps.getNamespaceURI());


                    Header holder = new Header(new QName(lastAdded.getNamespaceURI(),
                                                         lastAdded.getLocalName()),
                                                         lastAdded);
                    msg.getHeaders().add(holder);
                } else {
                    LOG.log(Level.WARNING, "IGNORE_NON_ELEMENT_REF_PARAM_MSG", o);
                }
            }
        }
    }
}
 
Example 6
Source File: CDAUtilities.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
public Element getlastChild(Element e) {
 Node n = e.getLastChild();
 while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
 	n = n.getPreviousSibling();
 return n == null ? null : (Element) n;
}