2

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

1
  • To Clarify, it's the string ' example.com/AttribElemClashes ' that I would want in this case - thank Commented May 11, 2011 at 9:19

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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());
             }

         }



   }

Comments

0

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?

2 Comments

Hi Brian, The XML is indeed fine - it's just some test code I grabbed as an example. My question is a more generic one. What do people think is the easiest and safeset way to retrieve the namespace from XML similar to this. - thanks, Rob
SAX is extremely fast, it fires an "event" method every time it hits an element eg: public void startElement(String namespaceURL, String localName, String qname, Attributes attributes). You can then do anything you like with this info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.