Example #1
0
  /**
   * Uses the XPath text() function to get values from <name> elements in received XML, then
   * collects those values as the value of a <names> element created here.
   *
   * <p>Demonstrates the following characteristics of the selectPath method:
   *
   * <p>- It supports expressions that include XPath function calls. - selectPath called from an
   * XmlCursor instance (instead of an XMLBeans type) places results (if any) into the cursor's
   * selection set.
   *
   * @param empDoc The incoming XML.
   * @return <code>true</code> if the XPath expression returned results; otherwise, <code>false
   *     </code>.
   */
  public boolean collectNames(XmlObject empDoc) {
    boolean hasResults = false;

    // Create a cursor with which to execute query expressions. The cursor
    // is inserted at the very beginning of the incoming XML, then moved to
    // the first element's START token.
    XmlCursor pathCursor = empDoc.newCursor();
    pathCursor.toFirstChild();

    // Execute the path expression, qualifying it with the namespace
    // declaration.
    pathCursor.selectPath(m_namespaceDeclaration + "$this//xq:employee/xq:name/text()");

    // If there are results, then go ahead and do stuff.
    if (pathCursor.getSelectionCount() > 0) {
      hasResults = true;

      // Create a new <names> element into which names from the XML
      // will be copied. Note that this element is in the default
      // namespace; it's not part of the schema.
      XmlObject namesElement = null;
      try {
        namesElement = XmlObject.Factory.parse("<names/>");
      } catch (XmlException e) {
        e.printStackTrace();
      }

      // Add a cursor the new element and put it between its START and END
      // tokens, where new values can be inserted.
      XmlCursor namesCursor = namesElement.newCursor();
      namesCursor.toFirstContentToken();
      namesCursor.toEndToken();

      // Loop through the selections, appending the incoming <name> element's
      // value to the new <name> element's value. (Of course, this could have
      // been done with a StringBuffer, but that wouldn't show the cursor in
      // use.)
      while (pathCursor.toNextSelection()) {
        namesCursor.insertChars(pathCursor.getTextValue());
        if (pathCursor.hasNextSelection()) {
          namesCursor.insertChars(", ");
        }
      }
      // Dispose of the cursors now that they're not needed.
      pathCursor.dispose();
      namesCursor.dispose();

      // Print the new element.
      System.out.println("\nNames collected by collectNames method: \n\n" + namesElement + "\n");
    }
    return hasResults;
  }
Example #2
0
  public void setNodeValue(String xpath, Object value) throws XmlException {
    xpath = initXPathNamespaces(xpath);

    XmlCursor cursor = xmlObject.newCursor();
    try {
      cursor.selectPath(xpath);

      if (cursor.toNextSelection()) {
        XmlUtils.setNodeValue(cursor.getDomNode(), value == null ? null : value.toString());
      }
    } finally {
      cursor.dispose();
    }
  }
 public void readHdrFtr() {
   bodyElements = new ArrayList<IBodyElement>();
   paragraphs = new ArrayList<XWPFParagraph>();
   tables = new ArrayList<XWPFTable>();
   // parse the document with cursor and add
   // the XmlObject to its lists
   XmlCursor cursor = headerFooter.newCursor();
   cursor.selectPath("./*");
   while (cursor.toNextSelection()) {
     XmlObject o = cursor.getObject();
     if (o instanceof CTP) {
       XWPFParagraph p = new XWPFParagraph((CTP) o, this);
       paragraphs.add(p);
       bodyElements.add(p);
     }
     if (o instanceof CTTbl) {
       XWPFTable t = new XWPFTable((CTTbl) o, this);
       tables.add(t);
       bodyElements.add(t);
     }
   }
   cursor.dispose();
   getAllPictures();
 }