Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java检索xml信息 美国地质勘探局所有地震,过去一周2020-02-26T21:28:38ZU.S。地质苏rveyhttps://earthquake.usgs.gov/https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.atomhttps://earthquake.usgs.gov/favicon.ico urn:地震美国地质勘探局政府:ci:39084495M 0.6-安扎西北13公里,CA2020-02-26T21:23:25.292ZTime2020-02-26 21:19:49 UTC200-02-26 13:19:49-08:00,震中位置33.602度;N 116.804和度;WDepth4.62公里(2.87英里)]]>33.6016667-116.8035-4620_Java_Xml - Fatal编程技术网

使用java检索xml信息 美国地质勘探局所有地震,过去一周2020-02-26T21:28:38ZU.S。地质苏rveyhttps://earthquake.usgs.gov/https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.atomhttps://earthquake.usgs.gov/favicon.ico urn:地震美国地质勘探局政府:ci:39084495M 0.6-安扎西北13公里,CA2020-02-26T21:23:25.292ZTime2020-02-26 21:19:49 UTC200-02-26 13:19:49-08:00,震中位置33.602度;N 116.804和度;WDepth4.62公里(2.87英里)]]>33.6016667-116.8035-4620

使用java检索xml信息 美国地质勘探局所有地震,过去一周2020-02-26T21:28:38ZU.S。地质苏rveyhttps://earthquake.usgs.gov/https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.atomhttps://earthquake.usgs.gov/favicon.ico urn:地震美国地质勘探局政府:ci:39084495M 0.6-安扎西北13公里,CA2020-02-26T21:23:25.292ZTime2020-02-26 21:19:49 UTC200-02-26 13:19:49-08:00,震中位置33.602度;N 116.804和度;WDepth4.62公里(2.87英里)]]>33.6016667-116.8035-4620,java,xml,Java,Xml,我试图从这个xml中提取信息,我确实做到了,但我不确定它是如何工作的,更确切地说,我不明白为什么这个值在节点中看起来如此之深。我使用的代码如下 <?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss"><title>USGS All Earthquakes, Past W

我试图从这个xml中提取信息,我确实做到了,但我不确定它是如何工作的,更确切地说,我不明白为什么这个值在节点中看起来如此之深。我使用的代码如下

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss"><title>USGS All Earthquakes, Past Week</title><updated>2020-02-26T21:28:38Z</updated><author><name>U.S. Geological Survey</name><uri>https://earthquake.usgs.gov/</uri></author><id>https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.atom</id><link rel="self" href="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.atom"/><icon>https://earthquake.usgs.gov/favicon.ico</icon>
<entry><id>urn:earthquake-usgs-gov:ci:39084495</id><title>M 0.6 - 13km WNW of Anza, CA</title><updated>2020-02-26T21:23:25.292Z</updated><link rel="alternate" type="text/html" href="https://earthquake.usgs.gov/earthquakes/eventpage/ci39084495"/><summary type="html"><![CDATA[<dl><dt>Time</dt><dd>2020-02-26 21:19:49 UTC</dd><dd>2020-02-26 13:19:49 -08:00 at epicenter</dd><dt>Location</dt><dd>33.602&deg;N 116.804&deg;W</dd><dt>Depth</dt><dd>4.62 km (2.87 mi)</dd></dl>]]></summary><georss:point>33.6016667 -116.8035</georss:point><georss:elev>-4620</georss:elev><category label="Age" term="Past Hour"/><category label="Magnitude" term="Magnitude 0"/><category label="Contributor" term="ci"/><category label="Author" term="ci"/></entry></feed>
builder=fac.newDocumentBuilder();
文档doc=builder.parse(源);
NodeList NodeList=doc.getDocumentElement().getChildNodes();
for(int i=0;i
我原以为在通过标记获取元素后,我应该能够立即获取值,但我必须深入两个层次,有人能解释为什么吗


编辑:输入错误你会问,为什么它看起来嵌套得这么深。我将首先回答这个问题,然后提出三种其他方法,您也可以考虑用java中的XML来处理。 为什么它如此嵌套

Element.getElementsByTagName()
返回包含0-n个元素的节点列表。您知道您只有一个,但xml引擎没有。所以你需要说:给我这个列表的第一个元素(
.item(0)

现在您有了元素
,但您希望有它的内容。您以子元素的身份访问内容,但同样:可能有多个子元素(与其他标记一样),因此您需要再次告知哪个元素

替代方法

  • 以文本内容的形式访问内容:
    element.getElementsByTagName(“georss:point”).item(0.getTextContent()

  • 使用Xpath:

    XPathFactory XPathFactory=XPathFactory.newInstance();
    XPath=xPathfactory.newXPath();
    XPathExpression expr=xpath.compile(“//*[local-name()='point']”);
    字符串evaluate=expr.evaluate(单据);
    系统输出打印LN(评估)

  • 请注意,xpath
    /*[local-name()='point']
    不是最好的方法。您应该使用需要更多代码的名称空间

  • 使用xml到bean的映射,例如
  • 你应该考虑这个方法,因为它基本上允许你拥有所有XML内容的POJO对象(如年龄、大小、……),并简单地将XML转换成这样的对象:

                builder = fac.newDocumentBuilder();
                Document doc = builder.parse(source);
                NodeList nodeList = doc.getDocumentElement().getChildNodes();
    
    
                for(int i=0;i < nodeList.getLength();i++){
                    Node node = nodeList.item(i);
                    if (node.getNodeName().equals("entry")){
                        Element element = (Element) node;
                        String nl1 = element.getElementsByTagName("georss:point").item(0).getChildNodes().item(0).getNodeValue();
                    }
                }
    

    我希望这有帮助。

    嗨,如果我的答案是你问题的解决方案,请相应地标记。如果没有,请指定仍然存在的问题。谢谢
    public class Entry{
        private String id;
        private String title;
        ...
        private String point;
        private String age;
        private String magnitude;
        ...