2

我正在尝试解析 android 中定义的 docx 文件,以将它们放在文本字段、图像视图和表格布局中。但我被困在孩子的抚养上。

文本文件以已知样式定义并分隔,该样式定义了主题的标题。但是所有主题都没有定义样式,我试图通过计算两个标题之间的内容并附加到正确的对象上来获得它们,

这是我的代码,但元素附加在同一个对象上

这是目标代码 + geters 和 seters

public class ObjectForPassingElements {

    Element name;
    Element content;

    int position;
    int order;
    int parrent;
    int id;
}


public List<ObjectForPassingElements> getElementByStyle(Document d,NodeList paragpraphList,String startTag,String tagname,String atribute,String atributeValue)
{

        Element paragraphElement;

        Element empty = d.createElement(startTag);
        Element elementWithTagName;
        List<ObjectForPassingElements> objListWithOutContent = new ArrayList<ObjectForPassingElements>();
        int start=0;
        int end = 0;
        int order = 0;

            for(int i=0;i<paragpraphList.getLength();i++)
            {

                paragraphElement = (Element) paragpraphList.item(i);
                //m.compareDocumentPosition(other);
                NodeList titleStyle = paragraphElement.getElementsByTagName(tagname);
                ObjectForPassingElements elementWithstyle = new ObjectForPassingElements();
                for(int j=0;j<titleStyle.getLength();j++)
                {
                    elementWithTagName = (Element) titleStyle.item(j);

                    if(elementWithTagName.getAttribute(atribute).equals(atributeValue))
                    {

                        elementWithstyle.setContent(empty);
                        elementWithstyle.setName((Element) paragpraphList.item(i));
                        elementWithstyle.setPosition(i);
                        elementWithstyle.setOrder(order);
                        objListWithOutContent.add(elementWithstyle);
                        order++;
                        //
                    }
                }
            }

            Element n;
            Node childBlankNode1 = d.createTextNode("\n\n");

        for(int i =0;i<paragpraphList.getLength();i++)
        {
            Log.d("in","inlist size"+objListWithOutContent.size()+"");
            for(int k=0;k<objListWithOutContent.size();k++)
            {
                start = objListWithOutContent.get(k).getPosition();

                if(k!=objListWithOutContent.size()-1)
                    end=objListWithOutContent.get(k+1).getPosition();
                else
                    end=paragpraphList.getLength()-1;

                if(i>=start&&i<end)
                {
                    n =(Element) paragpraphList.item(i);
                    objListWithOutContent.get(k).getContent().appendChild(n);
                }
            }
        }
        //

        for(ObjectForPassingElements obj : objListWithOutContent)
        {
            Log.d("fin",obj.getPosition()+" --- ///name "+obj.getName().getTextContent()+" name/// ---- ///content"+obj.getContent().getTextContent()+"///content  ");
        }

        return objListWithOutContent;
    }

这是 xml 文档表单 docx 文档的关闭示例

    <w:p>
<w:pStyle w:val="Protocols"/></w:pPr><w:r w:rsidRPr="00E37EC1"><w:t>HEADER 1</w:t></w:r>
</w:p>
<w:p><w:t>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id eros id nulla ultrices condimentum. Phasellus blandit pellentesque arcu, et placerat diam posuere id. Nunc feugiat, dui at pretium ultricies, enim mi placerat nibh, at bibendum urna massa id ipsum</w:t></w:p>
<w:p><w:t>Fusce dignissim feugiat sapien, a consectetur diam vestibulum quis. Pellentesque aliquam scelerisque erat, at auctor sem commodo sit amet. Cras eget enim sapien,</w:t></w:p>
<w:p><w:t>Aliquam elit leo, pretium ac suscipit rhoncus, eleifend vitae sem. Curabitur mattis euismod quam, vitae convallis elit sodales non.</w:t></w:p>
<w:p><w:t>Curabitur vel risus vitae lacus feugiat gravida in sed lorem. Proin fringilla vehicula nulla a consectetur. In hac habitasse platea dictumst. Proin viverra </w:t></w:p>
<w:p><w:t>Curabitur elementum condimentum enim tempus pharetra. Sed non nisl lectus. Aliquam vitae mi in diam vehicula aliquam a vitae tortor.</w:t></w:p>

<w:p>
<w:pStyle w:val="Protocols"/></w:pPr><w:r w:rsidRPr="00E37EC1"><w:t>HEADER 2</w:t></w:r>
</w:p>
<w:p><w:t>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. </w:t></w:p>
<w:p><w:t>Cras aliquam viverra lorem, eget aliquet ipsum malesuada ut</w:t></w:p>
<w:p><w:t>Etiam non est turpis, quis posuere mi. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In quis lectus sapien, nec ultricies arcu.</w:t></w:p>
<w:p><w:t>tent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam et tellus at libero dignissim lobortis ut vitae nisl. Mauris sol</w:t></w:p>
<w:p><w:t>etur in, molestie non dolor. Etiam in quam purus.</w:t></w:p>

以下是如何使用所需参数调用此函数

    public void stripXmlTags(File f) {

    Document d;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        FileInputStream is = new FileInputStream(f);

        d=db.parse(is);

        NodeList paragrph = d.getElementsByTagName("w:p");

        List<ObjectForPassingElements> protocolsList = new ArrayList<ObjectForPassingElements>();


        protocolsList = getElementByStyle(d,paragrph,"w:p","w:pStyle","w:val","Protocols");

……

4

0 回答 0