使用Dom Parser的Java中的额外括号

时间:2013-06-13 14:50:11

标签: 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>
    <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>

使用以下代码,但粗体结尾 parenthesess正在抛出所有错误:“语法错误,令牌}”。我仔细检查了括号。我在这里错过了一些明显的东西(抱歉,我是一个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();
    }

**}**

2 个答案:

答案 0 :(得分:1)

try {开始的地方似乎不在某个方法中。我的猜测是你希望它在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();
  }
 }
}

答案 1 :(得分:0)

你的试听挡块挂在空中。它应该是方法的一部分。在这种特殊情况下,似乎该方法应该是public static void main(String s[])。阅读更多相关信息here