gpt4 book ai didi

java - 使用 Java 从 XML 中提取数据

转载 作者:数据小太阳 更新时间:2023-10-29 02:22:22 25 4
gpt4 key购买 nike

我有以下 XML 代码:

<CampaignFrameResponse
xmlns="http://Qsurv/api"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Message>Success</Message>
<Status>Success</Status>
<FrameHeight>308</FrameHeight>
<FrameUrl>http://delivery.usurv.com?Key=a5018c85-222a-4444-a0ca-b85c42f3757d&amp;ReturnUrl=http%3a%2f%2flocalhost%3a8080%2feveningstar%2fhome</FrameUrl>
</CampaignFrameResponse>

我想做的是提取节点并将它们分配给一个变量。因此,例如,我有一个名为 FrameHeight 的变量,其中包含值 308

这是我目前的 Java 代码:

private void processNode(Node node) {
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
//calls this method for all the children which is Element
LOG.warning("current node name: " + currentNode.getNodeName());
LOG.warning("current node type: " + currentNode.getNodeType());
LOG.warning("current node value: " + currentNode.getNodeValue());
processNode(currentNode);
}
}

}

这会打印出节点名称、类型和值,但是将每个值分配给适当命名的变量的最佳方法是什么?例如 int FrameHeight = 308?

这是我更新的代码,其中 nodeValue 变量不断返回 null:

processNode(Node node) {
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
//calls this method for all the children which is Element
String nodeName = currentNode.getNodeName();
String nodeValue = currentNode.getNodeValue();
if(nodeName.equals("Message")) {
LOG.warning("nodeName: " + nodeName);
message = nodeValue;
LOG.warning("Message: " + message);
}
else if(nodeName.equals("FrameHeight")) {
LOG.warning("nodeName: " + nodeName);
frameHeight = nodeValue;
LOG.warning("frameHeight: " + frameHeight);
}
processNode(currentNode);
}
}

最佳答案

您可以使用 DOMSAXPull-Parser,但最好使用以下 API。

- JAXP 和 JAXB

- 蓖麻

例如:DOM 解析

DocumentBuilderFactory odbf = DocumentBuilderFactory.newInstance();
DocumentBuilder odb = odbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document odoc = odb.parse(is);
odoc.getDocumentElement().normalize (); // normalize text representation
System.out.println ("Root element of the doc is " + odoc.getDocumentElement().getNodeName());
NodeList LOP = odoc.getElementsByTagName("response");

Node FPN =LOP.item(0);
try{
if(FPN.getNodeType() == Node.ELEMENT_NODE)
{

Element token = (Element)FPN;

NodeList oNameList1 = token.getElementsByTagName("user_id");
Element firstNameElement = (Element)oNameList1.item(0);
NodeList textNList1 = firstNameElement.getChildNodes();
this.setUser_follower_id(Integer.parseInt(((Node)textNList1.item(0)).getNodeValue().trim()));
System.out.println("#####The Parsed data#####");
System.out.println("user_id : " + ((Node)textNList1.item(0)).getNodeValue().trim());
System.out.println("#####The Parsed data#####");

关于java - 使用 Java 从 XML 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12837430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com