我寫一個通用的(不是過於籠統實際;兩個假設:1>每個元素都必須是強制性的2>如果有多個段是存在的,那麼它應該正好發生'n'次)可以從CCB /平面文件生成XML的程序。 我提供了一個字符串輸入,考慮到作爲一個平面文件的內容,現在和XML配置是什麼,但XSD的XML格式的圖片。轉換的通用方式
我提供下列輸入:
<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
段,其具有兩次發生的情況完全一樣;對於第二段,我的代碼無法從輸入字符串中選擇字符,並將它們作爲相應元素節點的文本節點。
請幫助什麼是錯誤的邏輯。