2013-08-01 47 views
0

我需要将XSD Schema中的所有元素表示为XPath。有没有办法呢?就像考虑XSD Schema中有五个元素,我需要分别显示所有五个元素的XPath。 我的建议是在后台创建对应于XSD的XML,并且必须生成XPath。如果方法正确或建议采用其他方法,请提供相同的解决方案..是否有可能将XSD转换为Java中的XPath?

谢谢。 M.Sasi kumar

+0

如果可能的话,如果有人共享java程序将xpath转换为xsd,那将会很棒。我正在尝试。如果我得到解决方案,我会张贴相同的。 –

+0

您的问题要求将XSD指定为XPath,但您的上述注释要求将XPath指定为XSD。请澄清。如果您同时需要,请另外提出问题。 – kjhughes

回答

0
import java.io.File; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Stack; 
import javax.xml.parsers.*; 

import org.xml.sax.*; 
import org.xml.sax.helpers.DefaultHandler; 

/** 
* SAX handler that creates and prints XPath expressions for each element encountered. 
* 
* The algorithm is not infallible, if elements appear on different levels in the hierarchy. 
* Something like the following is an example: 
* - <elemA/> 
* - <elemA/> 
* - <elemB/> 
* - <elemA/> 
* - <elemC> 
* -  <elemB/> 
* - </elemC> 
* 
* will report 
* 
* //elemA[0] 
* //elemA[1] 
* //elemB[0] 
* //elemA[2] 
* //elemC[0] 
* //elemC[0]/elemB[1]  (this is wrong: should be //elemC[0]/elemB[0]) 
* 
* It also ignores namespaces, and thus treats <foo:elemA> the same as <bar:elemA>. 
*/ 

public class SAXCreateXPath extends DefaultHandler { 

    // map of all encountered tags and their running count 
    private Map<String, Integer> tagCount; 
    // keep track of the succession of elements 
    private Stack<String> tags; 

    // set to the tag name of the recently closed tag 
    String lastClosedTag; 

    /** 
    * Construct the XPath expression 
    */ 
    private String getCurrentXPath() { 
     String str = "//"; 
     boolean first = true; 
     for (String tag : tags) { 
      if (first) 
       str = str + tag; 
      else 
       str = str + "/" + tag; 
      str += "["+tagCount.get(tag)+"]"; 
      first = false; 
     } 
     return str; 
    } 

    @Override 
    public void startDocument() throws SAXException { 
     tags = new Stack(); 
     tagCount = new HashMap<String, Integer>(); 
    } 

    @Override 
    public void startElement (String namespaceURI, String localName, String qName, Attributes atts) 
     throws SAXException 
    { 
     boolean isRepeatElement = false; 

     if (tagCount.get(localName) == null) { 
      tagCount.put(localName, 0); 
     } else { 
      tagCount.put(localName, 1 + tagCount.get(localName)); 
     } 

     if (lastClosedTag != null) { 
      // an element was recently closed ... 
      if (lastClosedTag.equals(localName)) { 
       // ... and it's the same as the current one 
       isRepeatElement = true; 
      } else { 
       // ... but it's different from the current one, so discard it 
       tags.pop(); 
      } 
     } 

     // if it's not the same element, add the new element and zero count to list 
     if (! isRepeatElement) { 
      tags.push(localName); 
     } 

     System.out.println(getCurrentXPath()); 
     lastClosedTag = null; 
    } 

    @Override 
    public void endElement (String uri, String localName, String qName) throws SAXException { 
     // if two tags are closed in succession (without an intermediate opening tag), 
     // then the information about the deeper nested one is discarded 
     if (lastClosedTag != null) { 
      tags.pop(); 
     } 
     lastClosedTag = localName; 
    } 

    public static void main (String[] args) throws Exception { 
     if (args.length < 1) { 
      System.err.println("Usage: SAXCreateXPath <file.xml>"); 
      System.exit(1); 
     } 

     // Create a JAXP SAXParserFactory and configure it 
     SAXParserFactory spf = SAXParserFactory.newInstance(); 
     spf.setNamespaceAware(true); 
     spf.setValidating(false); 

     // Create a JAXP SAXParser 
     SAXParser saxParser = spf.newSAXParser(); 

     // Get the encapsulated SAX XMLReader 
     XMLReader xmlReader = saxParser.getXMLReader(); 

     // Set the ContentHandler of the XMLReader 
     xmlReader.setContentHandler(new SAXCreateXPath()); 

     String filename = args[0]; 
     String path = new File(filename).getAbsolutePath(); 
     if (File.separatorChar != '/') { 
      path = path.replace(File.separatorChar, '/'); 
     } 
     if (!path.startsWith("/")) { 
      path = "/" + path; 
     } 

     // Tell the XMLReader to parse the XML document 
     xmlReader.parse("file:"+path); 
    } 
} 
相关问题