简体   繁体   English

从XML标签读取特定的字符串

[英]Reading specific strings from XML tags

I am using java, I want to read strings from an XML tag. 我正在使用Java,我想从XML标签读取字符串。 EX: < blank type="Something"> I need to be able to assign "Something" to a variable. 例如:<blank type =“ Something”>我需要能够为变量分配“ Something”。 Any ideas? 有任何想法吗?

There are a lot of ways to do this: 有很多方法可以做到这一点:

  • You can use the XML APIs provided with Java (SAX or STAX or DOM). 您可以使用Java随附的XML API(SAX或STAX或DOM)。

  • There are libraries that build on the XML APIs (JDOM, DOM4J, or XOM) which are easier to use than the raw APIs. 有一些基于XML API(JDOM,DOM4J或XOM)构建的库,这些库比原始API易于使用。

  • There's Java-XML databinding, described in Pratik's answer . Pratik的答案中描述了Java-XML数据绑定。 Java-XML databinding is sometimes overkill, depending on your requirements, and when there are errors they can be hard to figure out. 根据您的要求,Java-XML数据绑定有时会过大,并且在出现错误时可能很难弄清。 Sometimes it's worthwhile, though. 不过有时候还是值得的。 I think JiBX is particularly interesting. 我认为JiBX特别有趣。

If you don't know where to start, start with XOM . 如果您不知道从哪里开始,请从XOM开始。 XOM was created by a JDOM contributor, it was designed to be easy to use. XOM是由JDOM贡献者创建的,旨在使其易于使用。

What you want to achieve is referred to as Unmarshalling an XML. 您想要实现的目标称为“ 解组 XML”。 Unmarshalling means extracting data from an XML document and using it to construct a Java object or graph of objects. 解组是指从XML文档中提取数据并使用它来构造Java对象或对象图。 There are various APIs available to do the same. 有多种可用的API可以执行相同的操作。 You should have a look at the following links: 您应该查看以下链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM