目前我递归地解析我的xml文档。我问是否有任何子节点,如果有,它将遍历子列表,并检查这些孩子是否有孩子。我的基本情况是它没有更多的孩子。现在解析是正确的我按正确的顺序得到一个大的节点列表。
我在想做的是让每个节点都成为自己的对象。 所以实体将是一个包含索引对象,列对象等的对象。 我的第一种思路是迭代循环,只需要一个switch语句检查它命中的每个节点,并创建将添加到实体的对象。我不认为一个大的转换声明是正确的。
这是xml的示例:
<ENTITY_DEFS>
<ENTITY name="WORKORDER">
<INDEXES>
<INDEX name="IDX_WO_1">WORKORDERID</INDEX>
<INDEX name="IDX_WO_2">WONUM, SITEID</INDEX>
<INDEX name="IDX_WO_3">ASSETNUM, SITEID</INDEX>
<INDEX name="IDX_WO_4">LOCATION, SITEID</INDEX>
</INDEXES>
<COLUMNS>
<COLUMN autokey="Y" autokeyprefix="LOCAL" parentkeycol="PARENTID" uniquekey="Y">WORKORDERID</COLUMN>
<COLUMN displaykey="Y" autokey="Y" autokeyprefix="OFFLN">WONUM</COLUMN>
<COLUMN>PARENTID</COLUMN>
<COLUMN>PARENT</COLUMN>
<COLUMN>DESCRIPTION</COLUMN>
<COLUMN>LONGDESCRIPTION</COLUMN>
<COLUMN>LOCATION</COLUMN>
<COLUMN>ASSETNUM</COLUMN>
<COLUMN>PERSONGROUP</COLUMN>
<COLUMN>CREWID</COLUMN>
<COLUMN>SUPERVISOR</COLUMN>
<COLUMN>LEAD</COLUMN>
<COLUMN>PHONE</COLUMN>
<COLUMN>REPORTEDBY</COLUMN>
<COLUMN>REPORTDATE</COLUMN>
<COLUMN>STATUS</COLUMN>
<COLUMN>STATUSDATE</COLUMN>
<COLUMN>WOPRIORITY</COLUMN>
<COLUMN>WORKTYPE</COLUMN>
<COLUMN>FAILURECODE</COLUMN>
<COLUMN>PROBLEMCODE</COLUMN>
<COLUMN>ISTASK</COLUMN>
<COLUMN>ORIGRECORDID</COLUMN>
<COLUMN>ESTDUR</COLUMN>
<COLUMN>OBSERVATION</COLUMN>
<COLUMN>MEASUREMENTVALUE</COLUMN>
<COLUMN>TASKID</COLUMN>
<COLUMN>SITEID</COLUMN>
<COLUMN>ORGID</COLUMN>
</COLUMNS>
<INIT_ACTION>
<ACTION>offline/getMyWorkOrderListJson.action</ACTION>
<ACTION>offline/getMyTaskWorkOrderListJson.action</ACTION>
</INIT_ACTION>
<DELTA_ACTION>
<ACTION>offline/getDeltaMyWorkOrderListJson.action</ACTION>
<ACTION>offline/getDeltaMyTaskWorkOrderListJson.action</ACTION>
</DELTA_ACTION>
<SYNC_ACTIONS transentityname="WORKORDER" transentitykey="WORKORDERID">
<ACTION name="INSERT" beanname="OfflineDataSyncAction" methodname="addWorkOrder"/>
<ACTION name="EDIT" beanname="OfflineDataSyncAction" methodname="editWorkOrder"/>
<ACTION name="INSERT_TASK" beanname="OfflineDataSyncAction" methodname="addTaskWorkOrder"/>
<ACTION name="UPDATE_STATUS" beanname="OfflineDataSyncAction" methodname="updateWorkOrderStatus"/>
<ACTION name="UPDATE_TASK" beanname="OfflineDataSyncAction" methodname="updateWorkOrderTask"/>
<ACTION name="UPDATE_TASK_STATUS" beanname="OfflineDataSyncAction" methodname="updateWorkOrderStatus"/>
<ACTION name="EMM_FILE_UPLOAD"/>
</SYNC_ACTIONS>
</ENTITY>
我的递归声明:
public void parse(final Document doc,final List<Node> nodelist, final Element el)
{
final NodeList children = el.getChildNodes();
for(int i = 0; i < children.getLength(); i++)
{
final Node node = children.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE)
{
nodelist.add(node);
parse(doc,nodelist,(Element)node);
}
}
}
答案 0 :(得分:1)
更优雅的方法是仅传递节点,因为您的nodelist
遍历单个对象。这样,您就可以避免使用冗余参数。
此外,您可以使el
成为您要捕获的nodename
的静态字符串:
public static void parse(Node node) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node currentNode = children.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
parse(currentNode);
}
if (node.getNodeName().equals(EL_NAME)) {
System.out.println("This -> " + node.getTextContent());
}
}