使用 SAX 字符方法从 XML 元素解析 PCDATA

Using the SAX characters method to parse PCDATA from an XML element

我正在使用 SAX API 在 xml 文档中进行解析,但很难从 XML.

中的每个位置存储元素 PCDATA

Oracle 文档 SAX API 显示 characters() 用于从元素解析 PCDATA,但我不确定应该如何调用它。

在我当前的实现中,布尔标志用于在遇到 XML 文档中的某个元素时发出信号。当遇到元素时,标志在 startElement() 中被触发。

我在 charaters() 中的布尔变量 description 上设置了一个断点,但布尔值直到调用 startElement() 才设置为真,这意味着 PCDATA 永远不会被解析。

我的问题是在 startElement() 中设置布尔值后如何调用 characters() ?

这是在 charaters():

之后调用的 startElement()
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        if (qName.equals("location")){
            location = true;

            System.out.println("Found a location...");
            try {
                //Read in the values for the attributes of the element <location>
                int locationID = Integer.parseInt(atts.getValue("id"));
                String locationName = atts.getValue("name");


                //Generate a new instance of Location on-the-fly using reflection. The statement Class.forName("gmit.Location").newInstance(); invokes the 
                //Java Class Loader and the calls the null (default) constructor of Location.
                Location loc = (Location) Class.forName("gmit.Location").newInstance();
                loc.setId(locationID); //Now configure the Location object with an ID, Name, Description etc...
                loc.setName(locationName);
                loc.setDescription(locationDescription);


            } catch (Exception e) {
                e.printStackTrace();
            }

        }else if (qName.equals("description")){
            description = true;
            //need to invoke the charaters method here after the description 
            //flag is set to true
            System.out.println("Found a description. You should tie this to the last location you encountered...");

    }

程序一启动就调用charaters(),但需要在上面的方法中设置boolean flags后调用:

public void characters(char[] ch,int start, int length) throws SAXException{
        if (location){

        }else if (description){

            locationDescription = new String( ch, start, length); 
            System.out.println("Description = " + locationDescription);

    }

XML 文件中某个位置的示例:

<location id="1" name="Tiberius">
        <description>
        You are in the city of Tiberius. You see a long street with high buildings and a castle.You see an exit to the south.
        </description>
        <exit title="Desert" direction="S"/>
    </location>

how can I call the characters() after the boolean values are set in startElement() ?

你不能。 SAX 解析的全部要点是解析器调用您的处理程序,而不是您调用解析器。

每次 SAX 解析器在文档中遇到字符数据时,都会调用您的 characters 方法。您的处理程序需要确定此数据是否相关(是位置、描述还是可以忽略的内容?)如果相关,请将此数据存储在以后可以检索的地方。

您向我们展示了您正在使用的 startElement 方法。如果您还没有这样做,您还需要覆盖 endElement。您需要在 endElement 方法中将布尔值 locationdescription 设置为 false,以便您的 SAX 处理程序知道它不再位于 location 中或 description 适当的元素。

您还没有向我们展示示例 XML 文档。也许你有这样的事情:

 <widgetList>
     <widget>
         <name>First widget</name>
         <location>Over there</location>
         <description>This is the first widget in the list</description>
     </widget>
     <widget>
         <name>Second widget</name>
         <location>Very far away</location>
         <description>This is the second widget in the list</description>
     </widget>
</widgetList>

如果是这样,您可能还想处理 widget 元素的末尾。例如,这可以采用处理程序遇到的最后位置和描述,将它们放在一个 Widget 对象中,并将其存储在处理程序内的某个列表中。在解析结束时,您可以从处理程序中读取小部件列表。