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

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

使用以下代码,但粗体结尾括号将全部抛出,并出现以下错误:“语法错误,标记}”。我仔细检查了括号。我是否在这里遗漏了一些明显的东西(抱歉我是一个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();
    }

**}**

最佳答案

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();
  }
 }
}

关于java - Java 中使用 Dom Parser 的额外括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17090150/

相关文章:

java - 当 ID 为数组形式时,如何从 SQL 中检索值

objective-c - 如何使用 NSScanner?

java - (Java) 如何解析具有多种可能格式的坐标字符串,并将它们转换为整数?

javascript - 对象数组没有被解析为原生对象的 JavaScript 数组?

java - 在 Hadoop 中解析 XML 文件

java - Gson - 解析具有不同字段名称和字段数量的 json

java - 如何根据用户输入隐藏和显示 Web View 前面的 View

xml - 如何构建 XML 解析器?

java - JAXB 无法编码类型 "java.lang.Class"

java - 如何处理 Org.Apache.Axiom.Om.OmException : Com. Ctc.Wstx.Exc.Wstxunexpectedcharexception : Illegal Character ((Ctrl-Char, 代码 25))