Solved

Java Filesystem manipulation of Word Docx file (find replace text)

Posted on 2016-10-24
3
113 Views
Last Modified: 2016-10-24
I am attempting to use a simple find and replace method using example code provided below. This is in Beanshell in my app. I am getting an error on the line indicated; Does so if I use 'env' or 'Null'! The logit lines indicate that 'env' is populated and the path coming from a form does exist. (using UNC path to network share).

Error: "Typed variable declaration : Attempt to resolve method: newFileSystem() on undefined variable or class name: FileSystems : at Line: 46 : in file: inline evaluation of: FileSystems .newFileSystem ( docxUri , null )"

Help very much appreciated.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

File zipfile = new File(thisProcess.getFieldValue("txt_Path"));
URI docxUri = new URI("jar:" + zipfile.toURI());

Map env = new HashMap();
importObject( env );
put("create", "false");
put("encoding", "UTF-8");

if(zipfile.exists() && !zipfile.isDirectory())
{
    logit.info("Document EXISTS");
}

logit.info(env);

try
{
    FileSystem zipFS = FileSystems.newFileSystem(docxUri, null); //ERROR ON THIS LINE
    Path documentXmlPath = zipFS.getPath("/word/document.xml");

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(Files.newInputStream(documentXmlPath));

    byte[] content = Files.readAllBytes(documentXmlPath);
    String xml = new String(content, StandardCharsets.UTF_8);
    xml = xml.replace("#DATE#", "2014-09-24");
    xml = xml.replace("#NAME#", StringEscapeUtils.escapeXml("Sniper"));
    content = xml.getBytes(StandardCharsets.UTF_8);
    Files.delete(documentXmlPath);
    Files.write(documentXmlPath, content);
}
0
Comment
Question by:BrightRaven
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 41856649
Not sure if Beanshell supports APIs of younger age. Why are you using it btw?
0
 

Author Comment

by:BrightRaven
ID: 41856656
Hi CEHJ

The IDE built into the BPM application I am using is either Javascript or Beanshell. (its very basic IDE).
The product is supposed to be extensible with full Java!

Just need the simplest method of finding and replacing text in a Docx and saving it back out with a new name. Was considering Docx4J (or similiar) if this method doesn't work.

Wise old owl guidance appreciated.

A.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 41856660
Was considering Docx4J (or similiar) if this method doesn't work.
That sounds more direct and less likely to fall over owing to complexity involving extra layers of interpretation
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java basic valueOf question 1 35
hashmap order 17 42
learn programming 8 72
Coding for the first time 9 70
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question