[英]Best way to read Namespace from XML stream (using Java)
I was wondering if people had some opinions on the following. 我想知道人们对以下内容是否有意见。
I have XML segments like: 我有如下XML段:
<?xml version="1.0" encoding="UTF-8"?>
<clashes:MatchingElementAndAttribute xmlns:clashes="http://example.com/AttribElemClashes" clash="123">
<clash>strval</clash>
</clashes:MatchingElementAndAttribute>
And I want to be able to extract the namespace of the XML fragment. 而且我希望能够提取XML片段的名称空间。
What is the best way of doing this (within Java) - and the most performant. 最好的方法是什么(在Java中),并且是性能最高的方法。
Thanks for any help and suggestions 感谢您的帮助和建议
Rob 抢
You can use stax parser like woodstox as it will perform well even with large XMLs. 您可以使用诸如woodstox之类的stax解析器,因为即使使用大型XML,它也可以很好地执行。 It loads XML as a stream and you will get event for start of the element.
它将XML作为流加载,并且您将获得元素开始的事件。 It also provides a way to get the QName (Qualified name) of the element as an object which also has the namespace available as a property.
它还提供了一种获取元素的QName(合格名称)作为对象的方法,该对象还具有可用的名称空间作为属性。
Have a look at http://www.xml.com/pub/a/2003/09/17/stax.html 看看http://www.xml.com/pub/a/2003/09/17/stax.html
reading XML that uses Namespaces. 读取使用命名空间的XML。 Please use the following code exactly, without any even little change.
请准确使用以下代码,而无需进行任何更改。
<?xml version="1.0" encoding="UTF-8" standalone="no"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:aapi="http://rdf.alchemyapi.com/rdf/v1/s/aapi-schema#" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:owl="http://www.w3.org/2002/07/owl#" xml:base="http://rdf.alchemyapi.com/rdf/v1/r/response.rdf">
<rdf:Description rdf:ID="d1dfa235105c033dec6dffdff63239d8b802087d9">
<rdf:type rdf:resource="http://rdf.alchemyapi.com/rdf/v1/s/aapi-schema#DocInfo"/>
<aapi:ResultStatus>OK</aapi:ResultStatus>
<aapi:Usage>By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html</aapi:Usage>
<aapi:URL/>
<aapi:Language>english</aapi:Language>
</rdf:Description>
<rdf:Description >
<aapi:Relevance>0.9683</aapi:Relevance>
<aapi:Name>Access control</aapi:Name>
<owl:sameAs rdf:resource="http://dbpedia.org/resource/Access_control"/>
<owl:sameAs rdf:resource="http://rdf.freebase.com/ns/guid.9202a8c04000641f8000000000051124"/>
</rdf:Description>
for the above XML , you can just use the following good Java code. 对于上述XML,您只需使用以下良好的Java代码即可。 I suggest you to not search in Google anymore before testing of this code by your own:
建议您在自己测试此代码之前,不要再在Google中搜索:
import javax.xml.parsers.*;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder docBuilder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = docBuilder.parse(new InputSource(new StringReader(strAbstractRdf)));
NodeList nl = doc.getElementsByTagNameNS("*","Description"); //the tag name
for (int kk=0;kk< nl.getLength(); kk++)
{
Node eDes = nl.item(kk);
if(eDes.getNodeType() == Node.ELEMENT_NODE)
{
Element eDescrition = (Element)eDes;
NodeList nlTermName= eDescrition.getElementsByTagNameNS("*","Relevance");
if(nlTermName.getLength() > 0)
{
Element eTermName =(Element) nlTermName.item(0);
System.out.println(eTermName.getTextContent());
}
}
}
You shouldn't see a clash here, the fact that your attribute and child element are both called "clash" really shouldn't be a problem. 您在这里不应该看到冲突,您的属性和子元素都被称为“冲突”这一事实确实不应该成为问题。
Do you have an existing parser running at all? 您是否正在运行现有的解析器? Is it having difficulty with this, eg throwing exceptions, failing to do what you expect?
这有困难吗,例如引发异常,没有按照您的期望去做?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.