Reputation: 35
I was wondering if people had some opinions on the following.
I have XML segments like:
<?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.
What is the best way of doing this (within Java) - and the most performant.
Thanks for any help and suggestions
Rob
Upvotes: 2
Views: 5512
Reputation: 2454
reading XML that uses Namespaces. 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. I suggest you to not search in Google anymore before testing of this code by your own:
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());
}
}
}
Upvotes: 1
Reputation: 1871
You can use stax parser like woodstox as it will perform well even with large XMLs. It loads XML as a stream and you will get event for start of the element. 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.
Have a look at http://www.xml.com/pub/a/2003/09/17/stax.html
Upvotes: 2
Reputation: 6450
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, e.g. throwing exceptions, failing to do what you expect?
Upvotes: 0