【发布时间】: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 段生成完全相同;对于第二段,我的代码无法从输入字符串中选择字符并将其作为相应元素节点的文本节点。
请帮忙看看逻辑有什么问题。
【问题讨论】: