【问题标题】:Conversion from Flat-file to XML in a generic way以通用方式从平面文件转换为 XML
【发布时间】:2017-05-27 18:05:59
【问题描述】:

我正在编写一个可以生成 XML 的通用程序(实际上不是太通用;两个假设:1> 每个元素都必须是强制性的 2> 如果有任何多个段,那么它应该恰好出现“n”次)程序来自建行/平面文件。 我提供了一个字符串输入,考虑到现在的平面文件的内容和一个配置 xml,它只不过是 xml 格式的 XSD 图片。

我在下面提供这些输入:

<complex name="PARENT">
<complex name="CHILD">
    <complex name="GRANT-CHILD" count="2">
        <field name="A" length="7"/>
        <field name="B" length="11"/>
        <field name="C" length="7"/>
        <field name="D" length="7"/>
        <field name="E" length="1"/>
        <field name="F" length="20"/>
        <field name="G" length="10"/>
        <field name="H" length="10"/>
        <field name="I" length="7"/>
        <field name="J" length="7"/>
        <field name="K" length="7"/>
        <field name="L" length="7"/>
    </complex>
</complex>

`

示例 XML 将如下所示:

<PARENT>
<CHILD>
    <GRANT-CHILD>
        <A />
        <B />
        <C />
        <D />
        <E />
        <F />
        <G />
        <H />
        <I />
        <J />
        <K />
        <L />
    </GRANT-CHILD>
    <GRANT-CHILD>
        <A />
        <B />
        <C />
        <D />
        <E />
        <F />
        <G />
        <H />
        <I />
        <J />
        <K />
        <L />
    </GRANT-CHILD>
</CHILD>

我的逻辑是,每当它是complex 类型时,我都会生成具有相应属性(name)的标签,当它是field 时,我正在寻找属性length 的值并从输入字符串中获取这些字符并在 xml 中制作一个标签,并将字符串中的这些字符替换为空格。我有两个类,提供如下:

package x.y.z;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class ChildEle {

public static Element getFirstChildElement(Node parent)
{
    Node child = parent.getFirstChild();
    while (child != null)
    {
        if (child.getNodeType() == Node.ELEMENT_NODE)
            return (Element)child;
        child = child.getNextSibling();
    }
    return null;
}
public static Node getNextSiblingElement(Node present)
{
    Node node = present.getNextSibling();
    while (node != null && !(node instanceof Element))
        node = node.getNextSibling();
    return node;
}
}

第二个是

package x.y.z;

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class FlatFileConversion {

public static void realmethod(Node node,String sto)
{
    if(node.getNodeName()=="complex")
    {   
        Element eElement = (Element)node;
        if(eElement.hasAttribute("count"))
        {
            String st=eElement.getAttribute("count");
            int x=Integer.parseInt(st);
            for(int i=0;i<x;i++)
            {
                System.out.println("<"+node.getAttributes().getNamedItem("name").getNodeValue()+">");
                realmethod((Node)ChildEle.getFirstChildElement(node),sto);
                System.out.println("</"+node.getAttributes().getNamedItem("name").getNodeValue()+">");
            }
        }
        else
        {
            System.out.println("<"+node.getAttributes().getNamedItem("name").getNodeValue()+">");
            realmethod((Node)ChildEle.getFirstChildElement(node),sto);
            System.out.println("</"+node.getAttributes().getNamedItem("name").getNodeValue()+">");
        }
    }
    if(node.getNodeName()=="field")
    {
        String str2=sto.substring(0, Math.min(sto.length(),Integer.parseInt(node.getAttributes().getNamedItem("length").getNodeValue())));
        System.out.print("<"+node.getAttributes().getNamedItem("name").getNodeValue()+">");
        System.out.print(str2.trim());
        System.out.println("</"+node.getAttributes().getNamedItem("name").getNodeValue()+">");
        sto=sto.replace(str2, "");
        try
        {
            realmethod(ChildEle.getNextSiblingElement(node),sto);
        }
        catch(Exception e)
        {

        }
    }
}
public static void main(String[] args) {

    String inp="74c83tjrl1nd7jmko3hg8octgitmicte3m0eq8mzmw7zae0sqgwrj4ylzueb9lzabc3hcu78lly3nwbi18ncw1mvu039ruvz5cju2vcyeq5upzsks9rn7jz75edrh2cbcxxh758ztvpkhyjb61al5eczc57bcizfoo1dhtdljd1gfzs69tqo9vqhiqt44gmbfdq7oddjfa";
    try
    {
         File inputFile = new File("E:\\test\\input.txt");
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         Document doc = dBuilder.parse(inputFile);
         doc.getDocumentElement().normalize();
         realmethod((Node)doc.getDocumentElement(),inp);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}

输出如下:

<PARENT>
<CHILD>
    <GRANT-CHILD>
        <A>74c83tj</A>
        <B>rl1nd7jmko3</B>
        <C>hg8octg</C>
        <D>itmicte</D>
        <E>3</E>
        <F>m0eq8mzmw7zae0sqgwrj</F>
        <G>4ylzueb9lz</G>
        <H>abchcu78ll</H>
        <I>ynwbi18</I>
        <J>ncw1mvu</J>
        <K>09ruvz5</K>
        <L>cju2vcy</L>
    </GRANT-CHILD>
    <GRANT-CHILD>
        <A>74c83tj</A>
        <B>rl1nd7jmko3</B>
        <C>hg8octg</C>
        <D>itmicte</D>
        <E>3</E>
        <F>m0eq8mzmw7zae0sqgwrj</F>
        <G>4ylzueb9lz</G>
        <H>abchcu78ll</H>
        <I>ynwbi18</I>
        <J>ncw1mvu</J>
        <K>09ruvz5</K>
        <L>cju2vcy</L>
    </GRANT-CHILD>
</CHILD>

出现两次的GRANT-CHILD 段生成完全相同;对于第二段,我的代码无法从输入字符串中选择字符并将其作为相应元素节点的文本节点。

请帮忙看看逻辑有什么问题。

【问题讨论】:

    标签: java xml dom


    【解决方案1】:

    没有真正回答您的问题,但了解一下可能会很有用,因为您正在解决同样的问题...

    开放网格论坛上有一个名为“数据格式定义语言”(DFDL) 的标准。 IBM 在其集成软件中实施了 DFDL: https://en.wikipedia.org/wiki/Data_Format_Description_Language

    并且有一个独立的开源实现可用: https://opensource.ncsa.illinois.edu/confluence/display/DFDL/Daffodil%3A+Open+Source+DFDL

    DFDL 可以描述平面文件,但也可以处理各种分隔和标记的数据。

    【讨论】:

      猜你喜欢
      • 2011-03-23
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 2012-07-01
      • 1970-01-01
      相关资源
      最近更新 更多