[英]Find the value of a specific attribute in an XML file in java
我只需要使用java讀取XML文件中單個屬性的值即可。 XML看起來像這樣:
<behavior name="Fred" version="2.0" ....>
我只需要讀出版本。 有人可以指出可以向我展示如何執行此操作的資源的方向嗎?
你並不需要一個奇特的圖書館-平原舊版本的JAXP DOM和XPath的是很容易的讀取和寫入這一點。 無論您做什么,都不要使用正則表達式。
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
public class GetVersion {
public static void main(String[] args) throws Exception {
XPath xpath = XPathFactory.newInstance().newXPath();
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse("file:////tmp/whatever.xml");
String version = xpath.evaluate("//behavior/@version", doc);
System.out.println(version);
}
}
為簡便起見, JAXB :
private static String readVersion(File file) {
@XmlRootElement class Behavior {
@XmlAttribute String version;
}
return JAXB.unmarshal(file, Behavior.class).version;
}
StAX的效率:
private static String readVersionEfficient(File file)
throws XMLStreamException, IOException {
XMLInputFactory inFactory = XMLInputFactory.newInstance();
XMLStreamReader xmlReader = inFactory
.createXMLStreamReader(new StreamSource(file));
try {
while (xmlReader.hasNext()) {
if (xmlReader.next() == XMLStreamConstants.START_ELEMENT) {
if (xmlReader.getLocalName().equals("behavior")) {
return xmlReader.getAttributeValue(null, "version");
} else {
throw new IOException("Invalid file");
}
}
}
throw new IOException("Invalid file");
} finally {
xmlReader.close();
}
}
這是一個 。
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import javax.xml.parsers.SAXParserFactory;
/**
* Here is sample of reading attributes of a given XML element.
*/
public class SampleOfReadingAttributes {
/**
* Application entry point
* @param args command-line arguments
*/
public static void main(String[] args) {
try {
// creates and returns new instance of SAX-implementation:
SAXParserFactory factory = SAXParserFactory.newInstance();
// create SAX-parser...
SAXParser parser = factory.newSAXParser();
// .. define our handler:
SaxHandler handler = new SaxHandler();
// and parse:
parser.parse("sample.xml", handler);
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
/**
* Our own implementation of SAX handler reading
* a purchase-order data.
*/
private static final class SaxHandler extends DefaultHandler {
// we enter to element 'qName':
public void startElement(String uri, String localName,
String qName, Attributes attrs) throws SAXException {
if (qName.equals("behavior")) {
// get version
String version = attrs.getValue("version");
System.out.println("Version is " + version );
}
}
}
}
如前所述,您可以使用SAXParser。
Digester提到使用正則表達式,我不建議這樣做,因為這會導致代碼難以維護:如果在另一個標簽或另一個行為標簽中添加另一個version屬性,該怎么辦? 您可以處理它,但它不會很漂亮。
您還可以使用XPath ,這是一種查詢xml的語言。 那就是我的建議。
如果您只需要閱讀版本,則可以使用正則表達式。 但實際上,我認為您需要使用Apache蒸煮器
Apache Commons Configuration也很好。 Commons Digester基於此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.