从HTTP端点读取XML内容,就像在Java中操作一个映射(map)一样。

huangapple go评论69阅读模式
英文:

read xml content from http endpoint like a map in Java

问题

URL url = new URL(endpoint);
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(url.openStream());
String latest;
while (reader.hasNext()) {
    if (reader.next() == XMLStreamConstants.START_ELEMENT) {
        if (reader.getLocalName().equals("valueId")) {
            latest = reader.getElementText();
            return latest;
        }
    }
}
英文:

How could I get value based on a key from an XML content http enpoint, so it is something like

<authority.result result="found 7 matches" startToken="xxxxxxx">
<TestEntry keyId="0right0" test="test" valueId="rightValue123" version="1"/>
<TestEntry keyId="0wrong" test="test" valueId="0wrongValue" version="1"/>
<TestEntry keyId="0wrong0" test="test" valueId="wrong" version="1"/>
</authority.result>

I would like to get the valueId when keyId=="0right0" only, I previously wrote following but could not get value for a specific key.

            URL url = new URL(endpoint);
            XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(url.openStream());
            String latest;
            while (reader.hasNext()) {
                if (reader.next() == XMLStreamConstants.START_ELEMENT) {
                    if (reader.getLocalName().equals("valueId")) {
                        latest = reader.getElementText();
                        return latest;
                    }
                }
            }

答案1

得分: 1

你需要区分XML元素和属性。要读取属性名称和值,你需要分别使用getAttributeNamegetAttributeValue方法。

以下是如何读取属性的示例代码:

import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

public class XmlStreamApp {
    public static void main(String[] args) throws IOException, XMLStreamException {
        ...
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(stream);
        Optional<String> value = findValueForTestEntry(reader, "0right0");
        System.out.println(value);
    }

    private static Optional<String> findValueForTestEntry(XMLStreamReader reader, String keyValue) throws XMLStreamException {
        while (reader.hasNext()) {
            if (reader.next() == XMLStreamConstants.START_ELEMENT) {
                String localName = reader.getLocalName();
                if ("TestEntry".equals(localName)) {
                    Optional<String> optionalValue = getValueForKey(reader, keyValue);
                    if (optionalValue.isPresent()) {
                        return optionalValue;
                    }
                }
            }
        }
        return Optional.empty();
    }

    private static Optional<String> getValueForKey(XMLStreamReader reader, String keyValue) {
        String value = "";
        boolean found = false;
        for (int attr = reader.getAttributeCount() - 1; attr >= 0; attr--) {
            QName attributeName = reader.getAttributeName(attr);
            if (attributeName.getLocalPart().equals("keyId")) {
                found = keyValue.equals(reader.getAttributeValue(attr));
            }
            if (attributeName.getLocalPart().equals("valueId")) {
                value = reader.getAttributeValue(attr);
            }
        }
        return found ? Optional.of(value) : Optional.empty();
    }
}

上述代码输出:

Optional[rightValue123]
英文:

You need to distinguish XML element from an attribute. To read attribute name and value you have to use getAttributeName and getAttributeValue methods respectively.

Below you find example code how to read attributes:

import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
public class XmlStreamApp {
public static void main(String[] args) throws IOException, XMLStreamException {
...
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(stream);
Optional&lt;String&gt; value = findValueForTestEntry(reader, &quot;0right0&quot;);
System.out.println(value);
}
private static Optional&lt;String&gt; findValueForTestEntry(XMLStreamReader reader, String keyValue) throws XMLStreamException {
while (reader.hasNext()) {
if (reader.next() == XMLStreamConstants.START_ELEMENT) {
String localName = reader.getLocalName();
if (&quot;TestEntry&quot;.equals(localName)) {
Optional&lt;String&gt; optionalValue = getValueForKey(reader, keyValue);
if (optionalValue.isPresent()) {
return optionalValue;
}
}
}
}
return Optional.empty();
}
private static Optional&lt;String&gt; getValueForKey(XMLStreamReader reader, String keyValue) {
String value = &quot;&quot;;
boolean found = false;
for (int attr = reader.getAttributeCount() - 1; attr &gt;= 0; attr--) {
QName attributeName = reader.getAttributeName(attr);
if (attributeName.getLocalPart().equals(&quot;keyId&quot;)) {
found = keyValue.equals(reader.getAttributeValue(attr));
}
if (attributeName.getLocalPart().equals(&quot;valueId&quot;)) {
value = reader.getAttributeValue(attr);
}
}
return found ? Optional.of(value) : Optional.empty();
}
}

Above code prints:

Optional[rightValue123]

答案2

得分: 0

你可以使用xpath来获取所需的值:

string(//TestEntry[@keyId="0right0"]/@valueId)

英文:

You could use an xpath to get to the desired value :

string(//TestEntry[@keyId=&quot;0right0&quot;]/@valueId)

huangapple
  • 本文由 发表于 2020年8月19日 05:03:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63476584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定