在Java中解析XML文件时出错

时间:2017-05-22 22:15:47

标签: java xml eclipse

我正在尝试解析属性的xml文件:

测试用例:名称&状态

testSteps:预期,实际和&状态

但是我收到了这个错误:

[Fatal Error] xmltestdata.xml:4:1635: The value of attribute "expected" associated with an element type "testSteps" must not contain the '<' character.

我会收到此错误的原因是什么?

爪哇

private static JiraIssue parseXML(String xmlFile) {
    try {

        //Read the XML File 
        File xmlFile1 = new File(xmlFile);
        DocumentBuilderFactory dbFactory
        DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xmlFile1);

        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + 
        doc.getDocumentElement().getNodeName());


    }

    finally {
    return null;

    }
}

XML(抱歉格式不佳)

<xml>
  <testSuite name="Suite: Test Reporter" startTime="05/22/2017 14:07:57" endTime="05/22/2017 14:07:57">

    <!-- name would have the (1) MSA ticket and (2) test status-->

    <testCase name="MSA001|en|us" status="Pass">

      <!-- below could be the (3) comment-->

      <testSteps name="Step 1" expected="Γεια σας. ξεκινήστε με τη UPS." actual="Γεια σας. ξεκινήστε με τη UPS." status="Pass" />
      <testSteps name="Step 2" expected="Αποστολή" actual="Αποστολή" status="Pass" />                     </testCase>

    <testCase name="MSA002|en|ca" status="Fail">


      <testSteps name="Step 1" expected="Bonjour. Démarrez avec UPS." actual="Bonjour. Démarrez avec UPS." status="Pass" />
      <testSteps name="Step 2" expected="Bonjour. Démarrez avec UPS." actual="Not found" status="Fail" />                     </testCase>

    <testCase name="MSA003|en|fr" status="Skip">

      <testSteps name="Step 1" expected="Dobrý den. začněte využívat služby UPS." actual="Αποστολή" status="Fail" />
      <testSteps name="Step 3" expected="Dobrý den. začněte využívat služby UPS." actual="Dobrý den. začněte využívat služby UPS." status="Pass" />                     </testCase>

    <testCase name="MSA004|en|ls" status="Fail">


      <testSteps name="Step 1" expected="Dobrý den. začněte využívat služby UPS." actual="Αποστολή" status="Fail" />
      <testSteps name="Step 2" expected="Dobrý den. začněte využívat služby UPS." actual="Dobrý den. začněte využívat služby UPS." status="Pass" />                     </testCase>

    <testCase name="MSA005|en|us" status="Fail">

      <testSteps name="Step 1" expected="<b>Γεια σας. ξεκινήστε με τη UPS.</b>" actual="Γεια σας. ξεκινήστε με τη UPS." status="Pass" />
      <testSteps name="Step 2" expected="Αποστολή" actual="Αποστολή" status="Fail" />                     </testCase>
  </testSuite>
</xml>`

2 个答案:

答案 0 :(得分:3)

错误在这里:

     <testSteps name="Step 1" expected="<b>Γεια σας. ξεκινήστε με τη UPS.</b>" actual="Γεια σας. ξεκινήστε με τη UPS." status="Pass" />

您在属性值中不能拥有<;它应该替换为&lt;

所以正确的行应该是

<testSteps name="Step 1" expected="&lt;b>Γεια σας. ξεκινήστε με τη UPS.&lt;/b>" actual="Γεια σας. ξεκινήστε με τη UPS." status="Pass" />

答案 1 :(得分:-1)

您可能会遇到编码问题。让我们尝试设置编码(我不知道这个xml中你的语言是什么,请尝试找到你的编码和设置)。 A reference to set encoding