Java 为什么实体名称必须紧跟在 XML 出现的实体引用中的“&”之后?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27729020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:55:42  来源:igfitidea点击:

Why must the entity name immediately follow the '&' in the entity reference appearing for the XML?

javaxml

提问by testing

I have a XML file called /NewFile.xmlin the D: directory. The XML file contains the following details:

我在 D: 目录中有一个名为/NewFile.xml的 XML 文件。XML 文件包含以下详细信息:

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

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

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

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

</prkpi>

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

</project>

The following code I'm running in eclipse as follows:

我在 eclipse 中运行的以下代码如下:

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 {
     WebDriver driver = new FirefoxDriver();
        A a = new A();
      
     Actions actions = new Actions(driver);
     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);

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

    
    
    }

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

 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) {
         Element eElement = (Element) nNode;
            ele=eElement.getElementsByTagName(tag).item(0).getTextContent();
        }
        return ele;
    }

}

When running this code I'm getting the following error in the console as follows:

运行此代码时,我在控制台中收到以下错误,如下所示:

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.
 at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
 at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
 at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
 at A.readXML(A.java:94)
 at A.main(A.java:40)

Please give me a solution for this issue..

请给我一个解决这个问题的方法..

采纳答案by The111

You have an unescaped &in there, just like @chrylis says the message says. ;-)

&在那里有一个未转义的人,就像@chrylis 说的那样。;-)

Change Business IAD & Business CPEto Business IAD &amp; Business CPE.

更改Business IAD & Business CPEBusiness IAD &amp; Business CPE

See also: What characters do I need to escape in XML documents?

另请参阅:我需要在 XML 文档中转义哪些字符?

回答by ajb

You cannot use an ampersand (&) directly in an XML file. Change this:

不能&在 XML 文件中直接使用与号 ( )。改变这个:

1523 Business IAD & Business CPE

to this:

对此:

1523 Business IAD &amp; Business CPE

The reason for the somewhat confusing error message is that when an XML processor sees &, it expects it to be immediately followed by an entity name or a "character reference", such as &amp;, &lt;, &#x225b, etc. So it saw the &in your name and didn't guess that you had just used it incorrectly.

究其原因,有些混乱的错误消息是,当XML处理器看到&,它希望它是紧跟着的一个实体的名称或“字符引用”,如&amp;&lt;&#x225b,等于是它看到了&您的姓名和没不要猜测你只是用错了它。