Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a shapefile, and information on its attributes is stored in a XML Document format. For example, the attribute of COUNTY is stored as follows:

-<attr>  
     <attrlabl>COUNTY</attrlabl>  
     <attrdef>County abbreviation</attrdef>  
     <attrtype>Text</attrtype>  
     <attwidth>1</attwidth>  
     <atnumdec>0</atnumdec>  
    -<attrdomv>  
        -<edom>  
            <edomv>C</edomv>  
            <edomvd>Clackamas County</edomvd>  
            <edomvds/>  
         </edom>  
        -<edom>  
            <edomv>M</edomv>  
            <edomvd>Multnomah County</edomvd>  
            <edomvds/>  
         </edom>  
        -<edom>  
            <edomv>W</edomv>  
            <edomvd>Washington County</edomvd>  
            <edomvds/>  
         </edom>  
     </attrdomv>  
 </attr>

When converting the shapefile to a PostGIS/PostgreSQL table, I also want to create a PostgreSQL/PostGIS table that will describe information on the attributes. So, the new table includes these columns: attribute(attrlabl), definition(attrdef), type(attrtype), width(attwidth), and categories(attrdomv).

I appreciate any suggestions.

share|improve this question
add comment

3 Answers 3

Parsing XML is always awkward, especially if there is the possibility of a range of different input formats (eg tags in capitals, mixed case or lower case). Therefore I would recommend using a parser such as BeautifulSoup, to perform the scraping of the data into python structures. From these structures you can write the data to the required format for your database.

This should be the most efficient way of doing this, as developing your own regexes for such a task is always much more work than you think it will be.

share|improve this answer
add comment

try this ( java ) :

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GetXML {


    private void showNodesValues( Node rootNode ) {

        Element rootElement = (Element) rootNode;
        NodeList postgis = rootNode.getChildNodes();
        int nodeCount = postgis.getLength();
        System.out.println(" > Childs of " + rootNode.getNodeName() + ": ");        
        for ( int x = 0; x < nodeCount; x++) {
            Node pgconfig = postgis.item(x);
            int nodeType = pgconfig.getNodeType();
            if ( nodeType == 1 ) {
                Element pgElement = (Element) pgconfig;
                String nodeName = pgElement.getNodeName();
                System.out.println( nodeName + " : " + getTagValue(nodeName, rootElement) );

                int childCount = pgconfig.getChildNodes().getLength();
                if ( childCount > 1 ) showNodesValues(pgconfig);
            }
        }


    }


    private String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        int count = nlList.getLength();
        if ( count > 0 ) {
            Node nValue = (Node) nlList.item(0);
            return nValue.getNodeValue();
        } else return "";
     }


    public void loadConfig( String file ) {
        try {
            File fXmlFile = new File(file);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();

            Node rootNode = doc.getElementsByTagName("attr").item(0);

            showNodesValues(rootNode);

          } catch (Exception e) {
                e.printStackTrace();
          }         
    }


}

public class Main {

    public static void main(String[] args) {

        GetXML xml = new GetXML();
        xml.loadConfig("c:/TheGrid/test.xml");
    }

}

The result looks like :

 > Childs of attr: 
attrlabl : COUNTY
attrdef : County abbreviation
attrtype : Text
attwidth : 1
atnumdec : 0
attrdomv :   

 > Childs of attrdomv: 
edom :   

 > Childs of edom: 
edomv : C
edomvd : Clackamas County
edomvds : 
edom :   

 > Childs of edom: 
edomv : M
edomvd : Multnomah County
edomvds : 
edom :   

 > Childs of edom: 
edomv : W
edomvd : Washington County
edomvds : 

If you can't go ahead, tell me with more specific details.

[]'s

share|improve this answer
    
This is useful for parsing ANY XML file. Change the root node name in doc.getElementsByTagName("attr"). –  Magno C Nov 26 '12 at 15:26
    
Is there any way to do the same task, but using PostgreSQL? Thanks, Magno. –  POTENZA Nov 26 '12 at 19:43
    
Humm. I dont think so ... SQL is a relational language to operate with data INSIDE the database. You will find some 3rd party to do this for you, like this one I show, but using your own code is more flexible. I just don't know in deep what you want to do. –  Magno C Nov 26 '12 at 20:09
add comment

You can use the PostGIS Shapefile loader and DBF exporter for shapefiles in general, so if you have PgAdmin III or some other PostGIS database you can host it on there. I'm not certain whether you can use the loader for only the XML file itself, but I do know you can do it for a Shapefile which more or less requires all the files associated with it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.