2015-01-01 6 views
6

Ho un file XML chiamato /NewFile.xml nella directory D :. Il file XML contiene i seguenti dati:Perché il nome dell'entità deve seguire immediatamente la '&' nel riferimento all'entità che appare per l'XML?

<?xml version="1.0"?> 
 
<project> 
 

 
<logindetails id="1001"> 
 
\t \t <url>https:xxx</url> 
 
     <username>xxx</username> 
 
     <password>xxx</password> 
 
</logindetails > 
 

 
<kpi id="1001"> 
 
\t \t <id_r>reports</id_r> 
 
\t \t <id_e>extranet</id_e> 
 
\t \t <id_pr>ext-pr</id_pr> 
 
</kpi> \t 
 

 
<prkpi id="1001"> 
 
     <id_1>ext-pr-backlog-age</id_1> 
 
\t \t <id_2>ext-timetoassign-prs</id_2> 
 
\t \t <id_3>ext-timetodeliver-prs</id_3> 
 
\t \t <id_4>ext-timetoresolve-prs</id_4> 
 
\t \t <id_5>ext-new-prs</id_5> 
 

 
</prkpi> 
 

 
<filtersection id="1001"> 
 
\t \t <visualizationId>Day,Week,Month,Quarter,Semester,Year,RD Tech Group,ICC,Center,Software Pack,Product,Project,Customer PRs,Severity,Priority</visualizationId> 
 
\t \t <projectId>dev/v4.3/r4/e12sqq,BATS,1523 Business IAD & Business CPE,[CoCo2M],VQM</projectId> 
 
</filtersection> 
 

 
</project>

Il seguente codice che sto correndo in Eclipse come segue:

import java.io.File; 
 
import java.io.IOException; 
 
import java.util.ArrayList; 
 
import java.util.List; 
 
import java.util.Properties; 
 

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

 

 
import org.apache.log4j.Logger; 
 
import org.apache.log4j.xml.DOMConfigurator; 
 
import org.openqa.selenium.By; 
 
import org.openqa.selenium.JavascriptExecutor; 
 
import org.openqa.selenium.WebDriver; 
 
import org.openqa.selenium.WebElement; 
 
import org.openqa.selenium.firefox.FirefoxDriver; 
 
import org.openqa.selenium.interactions.Actions; 
 
import org.openqa.selenium.support.ui.Select; 
 
import org.w3c.dom.Document; 
 
import org.w3c.dom.Element; 
 
import org.w3c.dom.Node; 
 
import org.w3c.dom.NodeList; 
 
import org.xml.sax.SAXException; 
 

 
public class A { 
 

 
    
 
    static Properties p= new Properties(); 
 
    String url=p.getProperty("url"); 
 
    private static Logger Log = Logger.getLogger(A.class.getName()); 
 

 
    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, InterruptedException { 
 
    \t WebDriver driver = new FirefoxDriver(); 
 
     A a = new A(); 
 
     
 
    \t Actions actions = new Actions(driver); 
 
    \t DOMConfigurator.configure("src/log4j.xml"); 
 
     String url = a.readXML("logindetails","url"); 
 
     String username = a.readXML("logindetails","username"); 
 
     String password = a.readXML("logindetails","password"); 
 
     
 
     //use username for webdriver specific actions 
 
     Log.info("Sign in to the OneReports website"); 
 
     driver.manage().window().maximize(); 
 
     driver.get(url); 
 
     Thread.sleep(5000); 
 
     Log.info("Enter Username"); 
 
     driver.findElement(By.id("loginUsername")).sendKeys(username); 
 
     Log.info("Enter Password"); 
 
     driver.findElement(By.id("loginPassword")).sendKeys(password); 
 
     //submit 
 
     Log.info("Submitting login details"); 
 
     waitforElement(driver,120 , "//*[@id='submit']"); 
 
     driver.findElement(By.id("submit")).submit(); 
 
     Thread.sleep(5000); 
 

 
    \t \t Log.info("Clicking on Reports link"); 
 
\t   String id_r = a.readXML("kpi","id_r"); 
 
\t   WebElement menuHoverLink = driver.findElement(By.id(id_r)); 
 
    \t \t  \t actions.moveToElement(menuHoverLink).perform(); 
 
    \t \t  \t Thread.sleep(6000); 
 
    \t \t  
 
    \t \t String id_e = a.readXML("kpi","id_e"); 
 
    \t \t WebElement menuHoverLink1 = driver.findElement(By.id(id_e)); 
 
    \t \t actions.moveToElement(menuHoverLink1).perform(); 
 
\t \t Thread.sleep(6000); 
 
\t \t  
 
\t \t String id_pr = a.readXML("kpi","id_pr"); 
 
\t \t WebElement menuHoverLink2 = driver.findElement(By.id(id_pr)); 
 
\t \t actions.moveToElement(menuHoverLink2).perform(); 
 
\t \t Thread.sleep(6000); 
 
\t \t  
 
\t \t String id_1 = a.readXML("prkpi","id_1"); 
 
\t \t WebElement menuHoverLink3 = driver.findElement(By.id(id_1)); 
 
\t \t actions.moveToElement(menuHoverLink3).click().perform(); 
 
\t \t Thread.sleep(6000); 
 

 
    
 
\t \t 
 
    } 
 

 
\t private static void waitforElement(WebDriver driver, int i, String string) { 
 
\t \t // TODO Auto-generated method stub 
 
\t \t 
 
\t } 
 

 
\t public String readXML(String searchelement,String tag) throws SAXException, IOException, ParserConfigurationException{ 
 
     String ele = null; 
 
     File fXmlFile = new File("D://NewFile.xml"); 
 
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
 
     Document doc = dBuilder.parse(fXmlFile); 
 
     doc.getDocumentElement().normalize(); 
 
     NodeList nList = doc.getElementsByTagName(searchelement); 
 
     Node nNode = nList.item(0); 
 
     if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
 
     \t Element eElement = (Element) nNode; 
 
      ele=eElement.getElementsByTagName(tag).item(0).getTextContent(); 
 
     } 
 
     return ele; 
 
    } 
 

 
}

Quando si esegue questo codice Sto ottenendo il seguente errore nella console come segue:

Fatal Error] NewFile.xml:27:57: The entity name must immediately follow the '&' in the entity reference. 
 
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:/D:/NewFile.xml; lineNumber: 27; columnNumber: 57; The entity name must immediately follow the '&' in the entity reference. 
 
\t at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) 
 
\t at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) 
 
\t at javax.xml.parsers.DocumentBuilder.parse(Unknown Source) 
 
\t at A.readXML(A.java:94) 
 
\t at A.main(A.java:40)

Vi prego di dare una soluzione a questo problema ..

+2

Post che file XML, o per lo meno la linea 27. Probabilmente avete un escape 'e' in là, proprio come dice il messaggio. – chrylis

+0

@chrylis Ho pubblicato il file XML completo – Amirdha

+0

Ho scoperto che sta accadendo a causa del projectId in xml che ha & .. Ma c'è un modo per analizzarlo in XML .. – Amirdha

risposta

1

Non è possibile utilizzare una e commerciale (&) direttamente in un file XML. Modificare questa:

1523 Business IAD & Business CPE 

a questo:

1523 Business IAD &amp; Business CPE 

Il motivo per il messaggio di errore un po 'di confusione è che quando un processore XML vede &, si aspetta di essere immediatamente seguito da un nome di un'entità o di un "riferimento carattere", ad esempio &amp;, &lt;, &#x225b, ecc. Quindi ha visto il & nel tuo nome e non ha indovinato che lo avevi usato in modo errato.

Problemi correlati