我正在编写一个通用(实际上不太通用;两个假设:1>每个元素都必须是强制性的2>如果存在任何多个段,那么它应该恰好出现“n”次)可以生成XML的程序来自 CCB/平面文件。 我提供一个字符串输入,将其视为目前平面文件的内容和一个配置 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 中创建一个标记,并将字符串中的这些字符替换为空格。我有两个类(class),提供如下:
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
出现两次的段生成的结果完全相同;对于第二段,我的代码无法从输入字符串中选取字符并将它们放置为相应元素节点的文本节点。
请帮忙看看逻辑上有什么问题。
最佳答案
并没有真正回答你的问题,但了解一下可能会有用,因为你正在解决同样的问题......
开放网格论坛上有一个名为“数据格式定义语言”(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 可以描述平面文件,但也可以处理各种分隔和标记数据。
关于java - 以通用方式从平面文件转换为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44220128/