[英]How to check with java, if a specific XML Element are existing in a XML Document
I have the following question: 我有以下问题:
I would like to check, whether a XML document contains a specific XML element. 我想检查XML文档是否包含特定的XML元素。 Is it possible to check, for example with a java method of a specific API, which returns a boolean value, wheter a specific XML element are available in a XML document?
是否可以检查(例如,使用特定API的java方法)返回布尔值,XML文档中是否有特定的XML元素?
This is my XML document as example: 这是我的XML文档示例:
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ServiceRequest>
<RequestPayload>
<LocationInformationRequest>
<InitialInput>
<GeoRestriction>
<Area>
<PolylinePoint>
<Longitude>11.0</Longitude>
<Latitude>12.0</Latitude>
<Altitude>13.0</Altitude>
</PolylinePoint>
</Area>
</GeoRestriction>
</InitialInput>
</LocationInformationRequest>
</RequestPayload>
</ServiceRequest>
</Test>
I need the information as a boolean value, wheter the XML element Area are existing or not existing. 我需要信息作为布尔值,而XML元素区域是否存在。 The XML Document is used in my own java classes as a type of string.
XML Document在我自己的java类中用作一种字符串。
Thanks for help ! 感谢帮助 !
public boolean isElementExists(String content) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
Document inputDoc = documentBuilderFactory.newDocumentBuilder().parse(new StringReader(content));
NodeList nodeList = inputDoc.getElementsByTagName(tagName);
return nodeList.getlength() == 0 ? true : false;
}
Since the document is a string and assuming you don't need to parse it for other reasons, I would use the StAX API : 由于文档是一个字符串,并且假设您不需要因其他原因解析它,我会使用StAX API :
public static boolean hasElement(String document, String localName)
throws XMLStreamException {
Reader reader = new StringReader(document);
XMLStreamReader xml = XMLInputFactory.newFactory()
.createXMLStreamReader(reader);
try {
while (xml.hasNext()) {
if (xml.next() == XMLStreamConstants.START_ELEMENT
&& localName.equals(xml.getLocalName())) {
return true;
}
}
} finally {
xml.close();
}
return false;
}
The advantage of this API over the SAX parse is that because it is a pull parser it is possible to stop processing the document before completion without using an artificial mechanism like throwing an exception. 这个API优于SAX解析的优势在于,因为它是一个拉解析器,所以可以在完成之前停止处理文档而不使用像抛出异常这样的人为机制。
You can execute XPath queries with Javax XPath (there are other XPath API's too): 您可以使用Javax XPath执行XPath查询(还有其他XPath API):
http://docs.oracle.com/javase/6/docs/api/javax/xml/xpath/package-summary.html http://docs.oracle.com/javase/6/docs/api/javax/xml/xpath/package-summary.html
If this is all u need, u could write ur own function: 如果这就是你需要的,你可以写你自己的功能:
If (xmlobjectstring.contains("<Area>") and xmlobjectstring.contains("</Area>") { }
Than u just need to parse out the different objects from the xmlfile. 比你只需要解析xmlfile中的不同对象。
You could also try out the SAX-XML Reader: 您还可以试用SAX-XML Reader:
http://blog.mynotiz.de/programmieren/java-sax-parser-tutorial-773/ http://blog.mynotiz.de/programmieren/java-sax-parser-tutorial-773/
If it gets more complicated you will need to use xpath: 如果它变得更复杂,你将需要使用xpath:
http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html
Two ways, you could try the Java Scanner class to look for a specific string (eg Longitude) assuming the existence of this word in the document implies the element exists. 有两种方法,您可以尝试使用Java Scanner类来查找特定字符串(例如Longitude),假设文档中存在此单词意味着该元素存在。
Or you could define a SAXParser that uses a custom handler to go through every element in the XML file but that may be too complicated for what you want to do. 或者您可以定义一个SAXParser,它使用自定义处理程序遍历XML文件中的每个元素,但这可能对您想要执行的操作而言过于复杂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.