5

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).

3 Answers 3

1

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.

0

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.

0

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.

0

Your Answer

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

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