gpt4 book ai didi

java - 使用 JAXB 解析 Xml 叶节点元素值

转载 作者:行者123 更新时间:2023-11-30 08:27:42 24 4
gpt4 key购买 nike

我有一个 xsd say request.xsd 和相应的 jaxb 生成的类。现在我得到了一个 xml 文件 request.xml,我可以解码并创建“请求”对象。

我在 xml 中有很多元素标签,其中一些可以多次使用。我需要创建一个应具有所有叶节点值的 java.util.List。

例如:

下面是我的 request.xml :

<Request>
<Operation>manual</Operation>
<Work>
<WorkModule>
<Name>AXN</Name>
</WorkModule>
</Work>
<Identifier>
<WorkStatus>
<WorkName>CCH</WorkName>
</WorkStatus>
<WorkStatus>
<WorkName>TMH</WorkName>
</WorkStatus>
</Identifier>
</Request>

下面是我的 JAXB 生成的请求类。同样每个xml元素对应的还有其他类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"Operation",
"Work",
"Identifier"
})
@XmlRootElement(name = "Request", namespace = "http://www.sprts.com/clm/nso/mahsgd")
public class Request{

@XmlElement(name = "Operation", required = true)
protected Operation operation;
@XmlElement(name = "Work", required = true)
protected Work work;
@XmlElement(name = "Identifier", required = true)
protected Identifier identifier;

\\ getters and setters
}

因此,使用 JAXB,我可以获得具有 xml 文件中所有值的未编码请求对象。

现在我如何以通用方式获取所有叶节点值(操作、名称、工作名称)而不使用来自请求对象的 getter,然后我可以将每个值放入某个集合中,比如说 List。我听说过 DOM 被用来做类似的事情,但我需要使用 JAXB 来做同样的事情。

(不使用来自请求对象的 getter,例如 String opertaion = request.getOperation();String name = request.getWork().getWorkModule().getName();)

--编辑--

有人可以帮我找到最佳解决方案吗?如果问题陈述不清楚,请告诉我。

--编辑--在 Doughan & Alexandros 的帮助下,周围的一些人也能够实现同样的目标。不确定解决方法(将 JAXB 对象转换为 DOM 对象再转换为 InputSource)是否是最佳解决方案。下面是工作代码。

     JAXBContext jc = JAXBContext.newInstance(JAXBObject.class);

// Create the Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();

// Marshal the Object to a Document
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(jaxbObject, document);

XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(document);
Result outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(xmlSource,outputTarget);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
InputSource source = new InputSource(is);

NodeList leafNodeObjects = (NodeList) xp.evaluate("//*[not(*)]", source, XPathConstants.NODESET);

for(int x=0; x<leafNodeObjects.getLength(); x++) {
System.out.print("nodeElement = ");
System.out.print(leafNodeObjects.item(x).getNodeName());
System.out.print(" and node value = ");
System.out.println(leafNodeObjects.item(x).getTextContent());
inputDtos.add(new InputDto(leafNodeObjects.item(x).getNodeName(),
leafNodeObjects.item(x).getTextContent()));
}

最佳答案

来自您的赏金评论:

I want to create a list of NodeObject where NodeObject has nodeElement and nodeValue property. exmaple. if I have an element like Anil then I will have one NodeObject for this element with nodeElement = name and nodeValue = property.

您可以使用以下 XPath 从任何 XML 文档中获取叶节点(参见:How to select all leaf nodes using XPath expression?):

//*[not(*)]

此处是使用 javax.xml.xpath API 进行操作:

import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

public class Demo {

public static void main(String[] args) throws Exception {
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();

InputSource xml = new InputSource("input.xml");
NodeList leafNodeObjects = (NodeList) xp.evaluate("//*[not(*)]", xml, XPathConstants.NODESET);

for(int x=0; x<leafNodeObjects.getLength(); x++) {
System.out.print("nodeElement = ");
System.out.print(leafNodeObjects.item(x).getNodeName());
System.out.print(" and node value = ");
System.out.println(leafNodeObjects.item(x).getTextContent());
}
}

}

下面是运行此演示代码的输出:

nodeElement = Operation and node value = manual
nodeElement = Name and node value = AXN
nodeElement = WorkName and node value = CCH
nodeElement = WorkName and node value = TMH

关于java - 使用 JAXB 解析 Xml 叶节点元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704332/

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