我正在尝试使用 DomParser 来解析具有如下输出的文件:
<XGuideWCSResponse xmlns="urn:com:x:presentationflow:spps:services:Xguide" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<XGuide xmlns="">
<startDate>2013-06-10-04:00</startDate>
<endDate>2013-06-16-04:00</endDate>
<locale>en_US</locale>
<Xs>
<X>
<XCode>A/O</XCode>
<XTitle>Ali Mushaf Oppurtunity Show</XTitle>
<positioningStatement xsi:nil="true"/>
<occurrences>
<scheduledX>
<startDate>2013-06-13-04:00</startDate>
<startTime>10:00:00.000-04:00</startTime>
<sourceCode>13061310</sourceCode>
<durationHours>1.0</durationHours>
<newShow>false</newShow>
<deferredPay>false</deferredPay>
<events/>
</scheduledX>
</occurrences>
</X>
</Xs>
</XGuide>
</XGuideWCSResponse>
使用以下代码,但粗体结束括号将其全部丢弃,并出现以下错误: "Syntax Error, on token } " 。我仔细检查了括号。我是否在这里遗漏了一些明显的东西(对不起,我是 java 新手 :))
/*
* Created on June 13, 2013
*
*/
package com.X.commerce.onair.commands;
import com.ibm.commerce.command.ControllerCommandImpl;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XPlusShowTitleControllerCmdImpl extends ControllerCommandImpl implements XPlusShowTitleControllerCmd {
// ...
protected Node getNode(String tagName, NodeList nodes) {
for ( int x = 0; x < nodes.getLength(); x++ ) {
Node node = nodes.item(x);
if (node.getNodeName().equalsIgnoreCase(tagName)) {
return node;
}
}
return null;
}
protected String getNodeValue( Node node ) {
NodeList childNodes = node.getChildNodes();
for (int x = 0; x < childNodes.getLength(); x++ ) {
Node data = childNodes.item(x);
if ( data.getNodeType() == Node.TEXT_NODE )
return data.getNodeValue();
}
return "";
**}**
protected String getNodeValue(String tagName, NodeList nodes ) {
for ( int x = 0; x < nodes.getLength(); x++ ) {
Node node = nodes.item(x);
if (node.getNodeName().equalsIgnoreCase(tagName)) {
NodeList childNodes = node.getChildNodes();
for (int y = 0; y < childNodes.getLength(); y++ ) {
Node data = childNodes.item(y);
if ( data.getNodeType() == Node.TEXT_NODE )
return data.getNodeValue();
}
}
}
return "";
}
protected String getNodeAttr(String attrName, Node node ) {
NamedNodeMap attrs = node.getAttributes();
for (int y = 0; y < attrs.getLength(); y++ ) {
Node attr = attrs.item(y);
if (attr.getNodeName().equalsIgnoreCase(attrName)) {
return attr.getNodeValue();
}
}
return "";
}
protected String getNodeAttr(String tagName, String attrName, NodeList nodes ) {
for ( int x = 0; x < nodes.getLength(); x++ ) {
Node node = nodes.item(x);
if (node.getNodeName().equalsIgnoreCase(tagName)) {
NodeList childNodes = node.getChildNodes();
for (int y = 0; y < childNodes.getLength(); y++ ) {
Node data = childNodes.item(y);
if ( data.getNodeType() == Node.ATTRIBUTE_NODE ) {
if ( data.getNodeName().equalsIgnoreCase(attrName) )
return data.getNodeValue();
}
}
}
}
return "";
}
try {
DOMParser parser = new DOMParser();
String UrlToParse = "http://www.theurlhere.com";
parser.parse(UrlToParse);
Document doc = parser.getDocument();
// Get the document's root XML node
NodeList root = doc.getChildNodes();
// Navigate down the hierarchy to get to the X node
Node comp = getNode("XGuide", root);
Node exec = getNode("X", comp.getChildNodes() );
String execType = getNodeAttr("type", exec);
// Load the executive's data from the XML
NodeList nodes = exec.getChildNodes();
String showTitleAttr = getNodeValue("XTitle", nodes);
System.out.println("X title is: " + showTitleAttr);
}
catch ( Exception e ) {
e.printStackTrace();
}
**}**