Java Code Examples for org.w3c.dom.Text#setNodeValue()

The following examples show how to use org.w3c.dom.Text#setNodeValue() . 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: J2SESampleProjectGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Extract nested text from an element.
 * Currently does not handle coalescing text nodes, CDATA sections, etc.
 * @param parent a parent element
 */
private static void replaceText(Element parent, String name) {
    NodeList l = parent.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        if (l.item(i).getNodeType() == Node.TEXT_NODE) {
            Text text = (Text)l.item(i);
            text.setNodeValue(name);
            return;
        }
    }
}
 
Example 2
Source File: JavaFXSampleProjectGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Extract nested text from an element.
 * Currently does not handle coalescing text nodes, CDATA sections, etc.
 * @param parent a parent element
 */
private static void replaceText(Element parent, String name) {
    NodeList l = parent.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        if (l.item(i).getNodeType() == Node.TEXT_NODE) {
            Text text = (Text) l.item(i);
            text.setNodeValue(name);
            return;
        }
    }
}
 
Example 3
Source File: XmlHandler.java    From oasp4j-ide with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves the variables within the element's Text and calls
 * {@link #resolveAttributes(NamedNodeMap)}
 * to resolve it's attributes.
 * @param element - element to be resolved.
 */
private void resolveElement(Element element) {
  resolveAttributes(element.getAttributes());
  NodeList nodeList = element.getChildNodes();

  for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    if (node instanceof Text) {
      Text text = (Text) node;
      text.setNodeValue(resolver.resolveVariables(text.getNodeValue()));
    }
  }
}
 
Example 4
Source File: XMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the node's value if it represents an element node.
 *
 * @param element the element
 * @param value the new value
 */
private void updateElement(final Element element, final Object value)
{
    Text txtNode = findTextNodeForUpdate(element);
    if (value == null)
    {
        // remove text
        if (txtNode != null)
        {
            element.removeChild(txtNode);
        }
    }
    else
    {
        final String newValue = String.valueOf(value);
        if (txtNode == null)
        {
            txtNode = document.createTextNode(newValue);
            if (element.getFirstChild() != null)
            {
                element.insertBefore(txtNode, element.getFirstChild());
            }
            else
            {
                element.appendChild(txtNode);
            }
        }
        else
        {
            txtNode.setNodeValue(newValue);
        }
    }
}
 
Example 5
Source File: ASIBaseClass.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 *
 *
 * @param xpath
 * @param fieldlabel
 */
protected void createFieldentry(String xpath, String fieldlabel)
{
  if(log.isDebugEnabled())
  {
    log.debug(
      "createFieldentry(String " + xpath + ", String " + fieldlabel + ")");
  }

  try
  {
    List qtimetadataNodes = this.selectNodes(xpath);
    if(qtimetadataNodes.size() > 0)
    {
      Node qtimetadataNode = (Node) qtimetadataNodes.get(0);
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document newDocument = db.newDocument();

      Element qtimetadataField =
        newDocument.createElement(QTIConstantStrings.QTIMETADATAFIELD);
      Element fieldlabelElement =
        newDocument.createElement(QTIConstantStrings.FIELDLABEL);
      Element fieldentryElement =
        newDocument.createElement(QTIConstantStrings.FIELDENTRY);

      Text fieldlabelText =
        newDocument.createTextNode(QTIConstantStrings.FIELDLABEL);
      fieldlabelText.setNodeValue(fieldlabel);
      fieldlabelElement.appendChild(fieldlabelText);

      Text fieldentryText =
        newDocument.createTextNode(QTIConstantStrings.FIELDENTRY);
      fieldentryElement.appendChild(fieldentryText);

      Node importedFLE =
        qtimetadataField.getOwnerDocument().importNode(
          fieldlabelElement, true);
      Node importedFEE =
        qtimetadataField.getOwnerDocument().importNode(
          fieldentryElement, true);
      qtimetadataField.appendChild(importedFLE);
      qtimetadataField.appendChild(importedFEE);
      Node importedField =
        qtimetadataNode.getOwnerDocument().importNode(qtimetadataField, true);
      qtimetadataNode.appendChild(importedField);
    }
  }
  catch(ParserConfigurationException pce) {
  	log.error("Exception thrown from createFieldentry()" + pce.getMessage(), pce);
  }
  catch(Exception ex)
  {
    log.error(ex.getMessage(), ex);
  }
}
 
Example 6
Source File: ASIBaseClass.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 *
 *
 * @param xpath
 * @param fieldlabel
 */
protected void createFieldentry(String xpath, String fieldlabel)
{
  if(log.isDebugEnabled())
  {
    log.debug(
      "createFieldentry(String " + xpath + ", String " + fieldlabel + ")");
  }

  try
  {
    List qtimetadataNodes = this.selectNodes(xpath);
    if(qtimetadataNodes.size() > 0)
    {
      Node qtimetadataNode = (Node) qtimetadataNodes.get(0);
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document newDocument = db.newDocument();

      Element qtimetadataField =
        newDocument.createElement(QTIConstantStrings.QTIMETADATAFIELD);
      Element fieldlabelElement =
        newDocument.createElement(QTIConstantStrings.FIELDLABEL);
      Element fieldentryElement =
        newDocument.createElement(QTIConstantStrings.FIELDENTRY);

      Text fieldlabelText =
        newDocument.createTextNode(QTIConstantStrings.FIELDLABEL);
      fieldlabelText.setNodeValue(fieldlabel);
      fieldlabelElement.appendChild(fieldlabelText);

      Text fieldentryText =
        newDocument.createTextNode(QTIConstantStrings.FIELDENTRY);
      fieldentryElement.appendChild(fieldentryText);

      Node importedFLE =
        qtimetadataField.getOwnerDocument().importNode(
          fieldlabelElement, true);
      Node importedFEE =
        qtimetadataField.getOwnerDocument().importNode(
          fieldentryElement, true);
      qtimetadataField.appendChild(importedFLE);
      qtimetadataField.appendChild(importedFEE);
      Node importedField =
        qtimetadataNode.getOwnerDocument().importNode(qtimetadataField, true);
      qtimetadataNode.appendChild(importedField);
    }
  }
  catch(ParserConfigurationException pce) {
  	log.error("Exception thrown from createFieldentry()" + pce.getMessage(), pce);
  }
  catch(Exception ex)
  {
    log.error(ex.getMessage(), ex);
  }
}