Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > XML, XSL > java+DOM+cdata


Автор: nogoody 29.10.2008, 12:01
DOM парсером в JAVA читаю xml

Как можно получить текст внутри элемента в котором содержится cdata без тегов cdata

пример

Код

...
<text><![CDATA[...test test test...]]></text>
...


функия элемента getTextContent()  выводит <![CDATA[...test test test...]]> 
должен же быть способ чтения без cdata

Автор: jsa 29.10.2008, 12:57
DOM в чей реализации?

Автор: nogoody 29.10.2008, 16:04
org.w3c.dom

Автор: jsa 30.10.2008, 05:13
Цитата(nogoody @  29.10.2008,  21:04 Найти цитируемый пост)
org.w3c.dom

это набор интерфейсов от sun, и может реализовать кто угодно

Цитата

Код

The attributes nodeName, nodeValue and attributes are included as a mechanism to get at node information without casting down to the specific derived interface. In cases where there is no obvious mapping of these attributes for a specific nodeType (e.g., nodeValue for an Element or attributes for a Comment ), this returns null. Note that the specialized interfaces may contain additional and more convenient mechanisms to get and set the relevant information. 

The values of nodeName, nodeValue, and attributes vary according to the node type as follows: 
Interface    nodeName              nodeValue                         attributes
CDATASection    "#cdata-section"    content of the CDATA Section    null



Цитата

getNodeValue
public String getNodeValue()
                    throws DOMException
The value of this node, depending on its type; see the table above. When it is defined to be null, setting it has no effect.

Автор: nogoody 30.10.2008, 12:11
пользуюсь стандартными средствами, велосипедов не изобретаю
Sun уже реализовала интерфейсы javax.xml.parsers и org.w3c.dom, входящих в состав пакета JAXP
Пример из книжки
Код


DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            saveDocument = builder.parse("test.xml");
Element element = saveDocument.getDocumentElement();
//Читаю
System.out.println(element.getElementsByTagName("insert").item(0).getFirstChild().getNodeValue());


На выходе,
<![CDATA[TEST]]>
Таже фигня если использовать TextContent
 smile 
Можно конкретнее, как именно, прочитать без cdata?

Автор: jsa 30.10.2008, 13:24
а nodeType проверь?

Добавлено через 4 минуты и 53 секунды
загляни http://www.exampledepot.com/egs/org.w3c.dom/pkg.html?l=rel#Getting%20Nodes, может пригодится

Автор: jsa 30.10.2008, 13:43
Цитата(nogoody @  30.10.2008,  17:11 Найти цитируемый пост)
System.out.println(element.getElementsByTagName("insert").item(0).getFirstChild().getNodeValue());

попробуй еще (на вскидку)
Код

element = element.getElementsByTagName("insert").item(0));

   CDATASection text1 = (CDATASection)element.getFirstChild();
    String string = text1.getData();


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)