Pregunta

i try to parse text from xml file and then write it and save in docx file with apachepoi XWPFDocument, it creates docx file, but it's empty, i cant seen there my text from parsed xml. Any suggestions will be appreciated?

xml:

 `<document>
  <el id="1">
    <text>Rakesh</text>
  </el>
  <el id="2">
    <text>John</text>
  </el>
  <el id="3">
    <text>Rajesh</text>
  </el>
</document>`

code:

public void dothis() throws ParserConfigurationException, SAXException,
        IOException, TransformerFactoryConfigurationError,
        TransformerException {

    in = new BufferedReader(new FileReader("D:\\Probe.xml"));
    XWPFDocument document1 = new XWPFDocument();
    XWPFParagraph paragraphOne = document1.createParagraph();
    XWPFRun paragraphOneRunOne = paragraphOne.createRun();

    paragraphOneRunOne.setText(in);
    PrintWriter zzz = new PrintWriter(new FileWriter("D:\\dd3.docx"));
    document1.write(zzz);
    zzz.close();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // Get the DOM Builder
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse("D:\\Probe.xml");
    List<Elementt> empList = new ArrayList<>();

    // Iteration durch den Knoten und die kinder Knoten extraktion
    NodeList nodeList = document.getDocumentElement().getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            Elementt emp = new Elementt();
            emp.id = node.getAttributes().getNamedItem("id").getNodeValue();

            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);

                // Unterelementen von xml identifizieren
                if (cNode instanceof Element) {
                    String content = cNode.getLastChild().getTextContent()
                            .trim();
                    switch (cNode.getNodeName()) {
                    case "text":
                        emp.text = content;
                        break;

                    }
                }
            }
            empList.add(emp);
        }
¿Fue útil?

Solución

Based on my experience with XWPFRun when you do this:

paragraphOneRunOne.setText(in);

The 'in' needs to equal the text you want to input. Your 'in' is equal to the following:

 in = new BufferedReader(new FileReader("D:\\Probe.xml"));

Try parsing the text first, then setting that as your character run, something like:

String in = textFromXMLFile
paragraphOneRunOne.setText(in);

Or, if I have understood your code correctly (I haven't done any xml yet), and the ArrayList contains your text, something like:

List<Elementt> empList = new ArrayList<>();
for(int i = 0; i < empList.length(); i++){
   paragraphOneRunOne.setText(empList.get(i));
}

The main point is whenever you set the runtext, whatever you use at that point seems to be what is inputted, so you need the relevant data ready before setting the run with it.

Good luck!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top