Java中使用Dom解析器的额外括号
我正在尝试使用DomParser来解析输出如下的文件:Java中使用Dom解析器的额外括号,java,parsing,xml-parsing,domparser,Java,Parsing,Xml Parsing,Domparser,我正在尝试使用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>
<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>
2013-06-10-04:00
2013-06-16-04:00
恩努斯
A/O
阿里·穆沙夫机会秀
2013-06-13-04:00
10:00:00.000-04:00
13061310
1
假的
假的
使用下面的代码,但粗体结尾的括号会抛出一个错误:“Syntax error,on token}”。我仔细检查了括号。我是否遗漏了一些显而易见的东西(对不起,我是java新手:)
/*
*创建于2013年6月13日
*
*/
包com.X.commerce.onair.commands;
导入com.ibm.commerce.command.ControllerCommandImpl;
导入org.apache.xerces.parsers.DOMParser;
导入org.w3c.dom.Document;
导入org.w3c.dom.NamedNodeMap;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
公共类XPlusShowTitleControllerCmdImpl扩展了ControllerCommandImpl实现了XPlusShowTitleControllerCmd{
// ...
受保护的节点getNode(字符串标记名、节点列表节点){
对于(int x=0;xtry{
的起始位置似乎不在方法中。我猜您希望它在main
方法中。将try{
到main
方法中最后一个
的行括起来
以下是修改后的程序:
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 "";
}
public static void main(String args[]) {
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();
}
}
}
公共类XplusShowTitleController-CmdImpl扩展了ControllerCommandImpl实现了XplusShowTitleController-Cmd{
// ...
受保护的节点getNode(字符串标记名、节点列表节点){
对于(int x=0;xpublic 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 "";
}
public static void main(String args[]) {
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();
}
}
}