2009-12-30 20 views
1

我將xml文件加載到DOM模型中並對其進行分析。閱讀從xml引用的轉義引用

該代碼是:

public class MyTest { 
public static void main(String[] args) {   
    Document doc = XMLUtils.fileToDom("MyTest.xml");//Loads xml data to DOM 
    Element rootElement = doc.getDocumentElement(); 
    NodeList nodes = rootElement.getChildNodes(); 
    Node child1 = nodes.item(1); 
    Node child2 = nodes.item(3); 
    String str1 = child1.getTextContent(); 
    String str2 = child2.getTextContent();  
    if(str1 != null){ 
     System.out.println(str1.equals(str2)); 
    } 
    System.out.println(); 
    System.out.println(str1); 
    System.out.println(str2); 
} 

}

MyTest.xml

<tests> 
    <test name="1">ff1 &quot;</test> 
    <test name="2">ff1 "</test> 
</tests> 

結果:

true 

ff1 " 
ff1 " 

期望的結果:

false 

ff1 &quot; 
ff1 " 

所以我需要區分這兩種情況:當報價被轉義並且不是。

請幫忙。

預先感謝您。

P.S.對於XMLUtils#fileToDom(字符串文件路徑),從XMLUtils類片段的代碼:

static { 
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 
    dFactory.setNamespaceAware(false); 
    dFactory.setValidating(false); 
    try { 
     docNonValidatingBuilder = dFactory.newDocumentBuilder(); 
    } catch (ParserConfigurationException e) { 
    } 
} 

public static DocumentBuilder getNonValidatingBuilder() { 
    return docNonValidatingBuilder; 
} 

public static Document fileToDom(String filePath) { 

    Document doc = getNonValidatingBuilder().newDocument(); 
    File f = new File(filePath); 
    if(!f.exists()) 
     return doc; 

    try { 
     Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
     DOMResult result = new DOMResult(doc); 
     StreamSource source = new StreamSource(f); 
     transformer.transform(source, result); 
    } catch (Exception e) { 
     return doc; 
    } 

    return doc; 

} 
+1

如果您不介意,你爲什麼需要這個?這個'''編碼只是爲了適合你的XML文檔,不屬於你的原始數據(將會是'&') – 2009-12-30 11:57:46

+0

http://stackoverflow.com/questions/1777878/is-there-a-java- xml-api-that-c​​an-parse-a-document-without-resolving-character-ent/1778304#1778304可能有幫助 – skaffman 2009-12-30 11:58:11

回答

1

我看了一下apache xerces的源代碼,並提出了我的解決方案(但它是猴子補丁)。 我已經寫了簡單的類

package a; 
import java.io.IOException; 
import org.apache.xerces.impl.XMLDocumentScannerImpl; 
import org.apache.xerces.parsers.NonValidatingConfiguration; 
import org.apache.xerces.xni.XMLString; 
import org.apache.xerces.xni.XNIException; 
import org.apache.xerces.xni.parser.XMLComponent; 

public class MyConfig extends NonValidatingConfiguration { 

    private MyScanner myScanner; 

    @Override 
    @SuppressWarnings("unchecked") 
    protected void configurePipeline() { 
     if (myScanner == null) { 
      myScanner = new MyScanner(); 
      addComponent((XMLComponent) myScanner); 
     } 
     super.fProperties.put(DOCUMENT_SCANNER, myScanner); 
     super.fScanner = myScanner; 
     super.fScanner.setDocumentHandler(this.fDocumentHandler); 
     super.fLastComponent = fScanner; 
    } 

    private static class MyScanner extends XMLDocumentScannerImpl { 

     @Override 
     protected void scanEntityReference() throws IOException, XNIException { 
      // name 
      String name = super.fEntityScanner.scanName(); 
      if (name == null) { 
       reportFatalError("NameRequiredInReference", null); 
       return; 
      } 

      super.fDocumentHandler.characters(new XMLString(("&" + name + ";") 
       .toCharArray(), 0, name.length() + 2), null); 

      // end 
      if (!super.fEntityScanner.skipChar(';')) { 
       reportFatalError("SemicolonRequiredInReference", 
         new Object[] { name }); 
      } 
      fMarkupDepth--; 
     } 
    } 

} 

您只需要在下一行添加到您的主要方法開始之前分析

System.setProperty(
      "org.apache.xerces.xni.parser.XMLParserConfiguration", 
      "a.MyConfig"); 

,你將有預期的結果:

false 

ff1 &quot; 
ff1 " 
0

貌似可以得到TEXT_NODE兒童和使用getNodeValue(假設它不是NULL):

public static String getRawContent(Node n) { 
    if (n == null) { 
     return null; 
    } 

    Node n1 = getChild(n, Node.TEXT_NODE); 

    if (n1 == null) { 
     return null; 
    } 

    return n1.getNodeValue(); 
} 

抓起即: http://www.java2s.com/Code/Java/XML/Gettherawtextcontentofanodeornullifthereisnotext.htm

+0

不,這不會讓你有任何不同。這意味着'生',但那不是。 – bmargulies 2010-01-01 23:27:17

0

對於內部實體沒有辦法做到這一點。 XML不支持這個概念。內部實體只是將相同的PSVI內容寫入文本的一種不同方式,它們並不是獨特的。