对于我目前正在做的一个小项目,我需要解析大量的“设置”文件并在某些条件下更改值。我相信这些文件是用 XML 格式化的,但是我开始怀疑这一点,因为我正在使用 Apache 的 Xerces XML 解析器来解析 java 中的文件并得到奇怪的结果......
这是我需要解析的示例“设置”文件(我提前申请了格式):
<LIBRARY_ITEM><NAME> AUsten(REdsockswhiteandblack), Kevin(Greysocks),
Johnny(REdandwhitesocksTall), David(REdandwhitesocksshort)(10)-Camera 1.avi
</NAME><ID>{DA656F16-CDDE-49C5-83B6-865DFB58356A}</ID><VERSION
subversion="1">2.0</VERSION><THUMBNAIL_INDEX>0</THUMBNAIL_INDEX><CATEGORIES>
<CATEGORY name="Skaters" id="{90a42bf0-58ec-46d4-8a54-8bbf7844d63a}">Johnny
Yaremko</CATEGORY><CATEGORY name="Skills" id="{165e7d21-aa8f-4376-b38b-
6fe20680e0d4}">Drop & Go</CATEGORY></CATEGORIES><CAMERA_NAME_NODE
CAMERA_NAME="Camera 1"/><TYPE>1</TYPE><LIBRARY_ITEM ItemType="Marker" IN="0"
UNIT="RefTime" OUT="0" SynchMarker="yes"><NAME></NAME><ID>{B02BA392-50D4-490C-9FDB-
0B7B350D2281}</ID><VERSION subversion="1">2.0</VERSION><FILE_NAME></FILE_NAME>
<Library.MDProperties><Property Name="Title" DefaultValue=""><![CDATA[Synch Point]]>
</Property></Library.MDProperties><Data Id="ODKeyPosition"><![CDATA[<ODKeyPosition
Version="1.0"><DrawingStream Value="1
0 13 BEGIN_SCENE_11052 3 0 1 0 65535 0 0 0 0
0 0.84375 0 1 0 1 720 9 END_SCENE"/></ODKeyPosition>]]></Data></LIBRARY_ITEM>
<CAMERA_NAME_NODE CAMERA_NAME="Camera 1"/><Library.MDLibraryItemLink><Reference
Id="TWIN"><LibraryItem Id="{74927B4E-00FF-4E12-B428-BF392E82CFA2}"
LastKnownLocation=".\ AUsten(REdsockswhiteandblack), Kevin(Greysocks),
Johnny(REdandwhitesocksTall), David(REdandwhitesocksshort)(10)-Camera 2.avi"/>
</Reference></Library.MDLibraryItemLink><OverlayDrawing><![CDATA[13 BEGIN_SCENE_11052 3
0 1 0 65535 0 0 0 0 0 0.84375 0 1 2 13501 3 0 1 0 65520 0 0 0 0 0 1 0 1 13 1105 3 0 1
0 65535 0 0 0 0 0 1 0 2 2 360 0 0 1 360 576 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127
127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 420 0 0 1 420 576 0 1 0 0
0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0
0 1 0 2 2 480 0 0 1 480 576 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3 2 0
20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 540 0 0 1 540 576 0 1 0 0 0 0 1 2 1 255
104 32 255 1 127 127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 600 0
0 1 600 576 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3 2 0 20 20 1 1105 3
0 1 0 65535 0 0 0 0 0 1 0 2 2 660 0 0 1 660 576 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127
127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 720 0 0 1 720 576 0 1
0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0
0 0 0 1 0 2 2 300 0 0 1 300 576 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3
2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 240 0 0 1 240 576 0 1 0 0 0 0 1 2 1
255 104 32 255 1 127 127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2
180 0 0 1 180 576 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3 2 0 20 20 1
1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 120 0 0 1 120 576 0 1 0 0 0 0 1 2 1 255 104 32 255
1 127 127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 60 0 0 1 60 576
0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535
0 0 0 0 0 1 0 2 2 0 0 0 1 0 576 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3
2 0 20 20 1 1 1 3 1 11 lineSpaceId19 Space between lines1 0 60 1 11 gridWidthId10 Grid
Width1 0 720 1 12 gridHeightId11 Grid Height1 0 576 0 1 1 13 orientationId16 Grid
Orientation1 0 1 0 0 0 13501 3 0 1 0 65520 0 0 0 0 0 1 0 1 9 1105 3 0 1 0 65535 0 0 0
0 0 1 0 2 2 0 288 0 1 720 288 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3 2
0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 0 348 0 1 720 348 0 1 0 0 0 0 1 2 1 255
104 32 255 1 127 127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 0
408 0 1 720 408 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3 2 0 20 20 1 1105
3 0 1 0 65535 0 0 0 0 0 1 0 2 2 0 468 0 1 720 468 0 1 0 0 0 0 1 2 1 255 104 32 255 1
127 127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 0 528 0 1 720 528
0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535
0 0 0 0 0 1 0 2 2 0 228 0 1 720 228 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255
0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 0 168 0 1 720 168 0 1 0 0 0 0 1 2
1 255 104 32 255 1 127 127 127 255 0 3 2 0 20 20 1 1105 3 0 1 0 65535 0 0 0 0 0 1 0 2
2 0 108 0 1 720 108 0 1 0 0 0 0 1 2 1 255 104 32 255 1 127 127 127 255 0 3 2 0 20 20 1
1105 3 0 1 0 65535 0 0 0 0 0 1 0 2 2 0 48 0 1 720 48 0 1 0 0 0 0 1 2 1 255 104 32 255
1 127 127 127 255 0 3 2 0 20 20 1 1 1 3 1 11 lineSpaceId19 Space between lines1 0 60 1
11 gridWidthId10 Grid Width1 0 720 1 12 gridHeightId11 Grid Height1 0 576 0 1 1 13
orientationId16 Grid Orientation1 0 0 0 0 0 1 720 9 END_SCENE]]></OverlayDrawing>
<Library.MDProperties><Property Name="CameraID" DefaultValue=""><![CDATA[0]]>
</Property><Property Name="Comment" DefaultValue=""><![CDATA[This is an acceleration
drill. The skater uses a wider stance than normal to achieve a shorter but more rapid
stride. This is required to get up to full speed in a hurry. Once at full speed a long
powerful stride will keep you there with the least amount of energy consumed. Body
position is once again important. Leaning too far forward will cause the skater to
loose traction as all their weight is not over their skates.]]></Property><Property
Name="Title" DefaultValue=""><![CDATA[ AUsten(REdsockswhiteandblack),
Kevin(Greysocks), Johnny(REdandwhitesocksTall), David(REdandwhitesocksshort)(10)-
Camera 1.avi]]></Property></Library.MDProperties></LIBRARY_ITEM>
如果有帮助的话,这些文件每个都对应一个 AVI 视频剪辑。我正在尝试使用一些条件语句一次编辑其中的许多,以便在引用它们的应用程序中更改剪辑属性。这是一个第三方应用程序,因此使这项任务对我来说相当困难,因为所有测试都必须在“黑盒”中完成(我不知道开发人员用来编写这些设置文件的方法/结构)。
我想我在问这些数据是否符合 XML 标准,或者它是否是一个完全不同的结构。这些文件中有一些地方看起来不像是正确的 XML...
[更新] 这是我用来扫描解析数据并将其打印到屏幕上的代码(以确保一切结构正确):
public Analyzer(String source) {
DOMParser parser = new DOMParser();
int level= 0;
Node curItem = null;
try {
parser.parse(source);
Document doc = parser.getDocument();
NodeList nodeList = doc.getElementsByTagName("LIBRARY_ITEM");
for (int i = 0; i < nodeList.getLength(); i++) {
read(nodeList.item(i), 0);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void read(Node node, int level) {
if (node == null) {
return;
}
int type = node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
System.out.print(node.getNodeName()+": ");
read(((Document) node).getDocumentElement(), level+1);
break;
}
case Node.TEXT_NODE: {
System.out.print(" = \""+node.getNodeValue().replaceAll("\\s", "")+"\"");
break;
}
case Node.ELEMENT_NODE: {
System.out.print("\n");
for (int i = 0; i < level; i++) {
System.out.print("\t");
}
System.out.print(node.getNodeName());
NodeList children = node.getChildNodes();
int length = children.getLength();
for (int i = 0; i < length; i++) {
read(children.item(i), level+1);
}
break;
}
}
}
当它运行时,它对一些字段做了一些时髦的事情......而且,一些值没有正确打印。这很容易成为我的错误,因为我对 XML非常缺乏经验。